PHP语言入门
PHP语言入门
2.1 PHP语言基础
2.1.1 PHP文件格式
Helloworld.php
//输出Hello,World
echo "Hello,World";
?>
2.1.2 PHP标记
……?>
echo ("script书写方法!");
echo("这是ASP的标记输出");
%>
echo("这是PHP的的简写标记输出");
?>
$expression=1;
if ($expression) {
?>
这是真的!
} else {
?>
这是假的!
}
?>
2.1.3 PHP 文件爱女的访问形式
PHP为服务端执行的语言,必须通过服务器解释才能执行
hello world!
2.1.4 PHP程序注释
echo "C++语言注释的方法即 //
"; // 采用C++的注释方法
/* 多行注释
对于大段的注释很有用的哦*/
echo "多行注释方法即 /*...*/
";
echo "Unix的注释方法即 #
"; # 使用UNIX Shell语法注释
?>
/*
echo "不能嵌套使用多行注释符号\n"; /* 嵌套使用会出错*/
*/
?>
2.1.5 PHP语法概述
类似于C/C++
2.2 PHP的变量
2.2.1 PHP的变量命名
PHP变量名是区分大小写的,和C语言是一致的;
变量名必须是以美元符号($)开始;
变量名开头可以以下划线开始;
变量名不能以数字字符开始。
2.2.2 PHP的数据类型
$True="我是变量" . True;
echo($True);
echo("
");
echo("我是关键字" . True)
?>
$int_D=2147483647; //十进制赋值
echo($int_D);
echo("
");
$int_H=0x7FFFFFFF; //十六进制赋值
echo($int_H);
echo("
");
$int_O=017777777777; //八进制赋值
echo($int_O);
echo("
");
?>
$float_1=90000000000;
echo($float_1);
echo("
");
$float_2=9E10;
echo($float_2);
echo("
");
$float_3=9E+10;
echo($float_3);
?>
$single_str='我被单引号括起来了!
';
echo $single_str;
$single_str='输出单引号:\'嘿嘿,我在单引号里面\'
';
echo $single_str;
$single_str='输出双引号:"我在双引号里面"
';
print $single_str;
$single_str='输双美元符号:$';
print $single_str;
?>
$Double_str="我被双引号括起来了!
";
echo $single_str;
$single_str="输出单引号:'嘿嘿,我在单引号里面'
"; //不需要转义符
echo $single_str;
$single_str="输出双引号:\"我在双引号里面\"
"; //需要转义符
print $single_str;
$single_str="输出美元符号:\$
"; //需要转义符
print $single_str;
$single_str="输出反斜杠:\\
"; //需要转义符
print $single_str;
?>
$heredoc_str =
你好
美元符号 $
反斜杠 \
"我爱你"
'我恨你'
heredoc_mark;
echo $heredoc_str;
?>
本实例演示字符串中对变量的引用:
$str_1 = "我是变量的值!";
$str_2 = "str_1 : $str_1
"; //双引号字符串中包含变量$str_1
echo $str_2;
$str_1 = '我是变量的值';
$str_2 = 'str_1 : $str_1
'; //单引号中包含字符串的值
echo $str_2;
$str_1 = "我是变量的值!";
$str_2 = "str_1 : $str_12
"; //引用的变量名后,多了个字符即$str_12
echo $str_2;
$str_1 = "我是变量的值!";
$str_2 = "str_1 : ${str_1}2
"; //引用的变量名后,多了个字符即$str_12
echo $str_2;
?>
$a=""; //空字符串
$b = 3.1; //浮点型
$c= TRUE; //布尔型
$d = 2147483647; //整型
$e = 2147483648; //超过整型的范围,自动编程浮点型
$f= 0x80000000 ; //十六进制
$g="hi"; //字符串
$h=array(1,2); //数组
//分别输出类型
echo '$a类型是:';
var_dump($a);
echo '
$b类型是:';
var_dump($b);
echo '
$c类型是:';
var_dump($c);
echo '
$d类型是:';
var_dump($d);
echo '
$e类型是:';
var_dump($e);
echo '
$f类型是:';
var_dump($f);
echo '
$g类型是:';
var_dump($g);
echo '
$h类型是:';
var_dump($h);
?>
$a=0.1;
$b='hi';
$c=True;
if (is_numeric($a))
echo '$a是数值型
';
else
echo '$a不是数值型
';
if (is_int($a))
echo '$a是整型
';
else
echo '$a不是整型
';
if (is_string($b))
echo '$b是字符串型
';
else
echo '$b不是字符串型
';
if (is_bool($c))
echo '$c是布尔型
';
else
echo '$c不是布尔型
';
?>
$a=0.1; //定义变量
$b=0; //0值
$c=""; //空字符串
if (isset($a))
echo '$a已经被定义
';
else
echo '$a没有被定义
';
if (!empty($a))
echo '$a不为空
';
else
echo '$a为空
';
if (isset($b))
echo '$b已经被定义
';
else
echo '$b没有被定义
';
if (!empty($b))
echo '$b不为空
';
else
echo '$b为空
';
if (isset($c))
echo '$c已经被定义
';
else
echo '$c没有被定义
';
if (!empty($c))
echo '$a不为空
';
else
echo '$a为空
';
if (isset($d))
echo '$d已经被定义
';
else
echo '$d没有被定义
';
if (!empty($d))
echo '$d不为空
';
else
echo '$d为空
';
?>
2.2.3 数据类型转换
echo gettype((bool) "") . "
"; // bool(false)
echo gettype((bool) 1) . "
"; // bool(true)
echo gettype((bool) -2) . "
"; // bool(true)
echo gettype((bool) "foo") . "
"; // bool(true)
echo gettype((bool) 2.3e5) . "
"; // bool(true)
echo gettype((bool) array(12)) . "
"; // bool(true)
echo gettype((bool) array()) . "
"; // bool(false)
?>
$a=123456;
$b=123.456;
$c=-1.3e8;
//通过echo函数输出时,自动转换成
echo $a . "abc
";
echo $b ."cde
";
echo $c . "abc
";
//字符串转换成数值
$foo = 1 + "10.5";
echo $foo . "
";
$foo = 1 + "-1.3e3";
echo $foo ."
";
$foo = 1 + "bob-1.3e3";
echo $foo ."
";
$foo = 1 + "bob3";
echo $foo ."
";
$foo = 1 + "10 Small Pigs";
echo $foo ."
";
$foo = 4 + "10.2 Little Piggies";
echo $foo ."
";
$foo = "10.0 pigs " + 1;
echo $foo ."
";
$foo = "10.0 pigs " + 1.0;
echo $foo ."
";
$foo = 1 + "010";
echo $foo ."
";
$foo = 1 + "0x10";
echo $foo ."
";
$foo = 1 + "\x10";
echo $foo ."
";
$bar = 1 + "\x35"; //0x35 is ASCII for '5'
echo $foo ."
";
$foo = 1 + "0x\x41\x31"; //0x41 is ASCII 'A'; 0x31 is '1'
echo $foo ."
";
?>
2.2.4 PHP中的预订义变量
echo '提交方法为:'. $REQUEST_METHOD;
echo '
网络协议为:' .$SERVER_PROTOCOL;
?>
echo '主机名称:'.$HTTP_SERVER_VARS['HTTP_HOST'];
echo '
服务器根路径:'.$HTTP_SERVER_VARS['DOCUMENT_ROOT'];
?>
echo '当前执行脚本文件:'.$_SERVER['PHP_SELF'];
echo '
当前执行脚本文件绝对路径:'.$_SERVER['SCRIPT_FILENAME'];
?>
2.2.5 变量的引用
$myName="little";
$YourName=&$myName; //引用变量
$myName="littlePig
";
print($YourName);
$YourName="littleCat
";
print($myName);
?>
2.2.6 变量的变量
$myStr="myName"; //设定变量
$$myStr="Marry"; //设定变量的变量即$$myStr
echo $myStr; //输出变量
echo "
";
echo $$myStr; //输出变量的变量
echo "
";
echo $myName; //直接输出$myName和使用$$mystr结果一样
echo "
";
$myStr="yourName";
if (!isset($$myStr))
echo '$$myStr变量不存在呢';
?>
2.2.7 常量
define("CONSTANT", "Hello world\n");
echo CONSTANT . "
"; // 输出"Hello world."
echo Constant . "
"; // 输出"Constant" ,表示没有该常量
define("GREETING", "Hello you\n", true);
echo GREETING. "
"; // 输出"Hello you."
echo Greeting. "
"; // 输出"Hello you.",因为设定大小写不敏感
define("MAXSIZE", "100\n");
echo MAXSIZE; //输出
echo constant("MAXSIZE"). "
"; // 输出
echo(defined("ONE")). "
";
define("ONE","Hello,One");
echo(defined("ONE")). "
"; //如果定义返回True,使用echo输出显示
?>
echo "程序当前行:". __LINE__;
echo "
操作系统:".PHP_OS;
echo "
PHP版本:" .PHP_VERSION
?>
2.3 PHP的运算符
2.3.1 算术运算符
$ONE= 1;
$TWO= 2;
$TREE= 3;
echo $ONE+$TWO . "
"; //加
echo $ONE-$TWO . "
"; //减
echo $ONE*$TWO . "
"; //乘
echo $ONE/$TWO . "
"; //除
echo $TREE%$TWO. "
"; //取余数
?>
2.3.2 赋值运算符
$a= 2;
$a +=3; //$a=$a+3,值为
echo $a . "
";
$a -=3; //$a=$a-3,值为
echo $a . "
";
$a *=3; //$a=$a*3,值为
echo $a . "
";
$a /=3; //$a=$a/3,值为
echo $a . "
";
?>
2.3.3 位运算符
$a= 7;
$b= 14;
$myVal= $a & $b; //位与
echo $myVal. "
";
$myVal=$a | $b; //位或
echo $myVal. "
";
$myVal= $a ^ $b; //位异或
echo $myVal. "
";
$myVal= ~$a; //为取反
echo $myVal. "
";
?>
2.3.4 三元运算符
2.3.5 比较运算符
echo("10小于吗:" . ((10");
echo("10小于等于吗:" . ((10");
echo("10大于吗:" . ((10>10)?"True":"False") . "
");
echo("10大于等于吗:" . ((10>=10)?"True":"False") . "
");
echo("10等于吗:" . ((10==10)?"True":"False") . "
");
echo("10不等于吗:" . ((10!=10)?"True":"False") . "
");
?>
2.3.6 字符串运算符
$a="Hello ";
$b="World";
$ab=$a . $b ."
"; //字符串连接符号.
echo $ab;
$a="some";
$a .="thing" . "
"; //赋值运算符.=
echo $a;
$a="24 five 6" + 6;
echo $a . "
";
$a="24five 6" + 6;
echo $a . "
";
$a="five 6" + 6;
echo $a . "
";
$a="24 five" . 6;
echo $a . "
";
$a="24five" . 6;
echo $a . "
";
?>
2.3.7 递增和递减运算符
$a=20;
echo "a++:" . $a++ ."
"; //后加
echo "变量a的新值:" . $a ."
";
$a=20; //重新赋值
echo "++a:" . ++$a ."
"; //先加
echo "变量a的新值:" . $a ."
";
$a=20; //重新赋值
echo "a--:" . $a-- ."
"; //后减
echo "变量a的新值:" . $a ."
";
$a=20; //重新赋值
echo "--a:" . --$a ."
"; //先减
echo "变量a的新值:" . $a ."
";
?>
2.3.8 逻辑运算符
2.3.9 运算符优先级
2.4 表达式
2.5 控制语句
2.5.1 条件语句
$achievement=91;
if ($achievement
echo "你不及格";
elseif ($achievement>=60 && $achievement
echo "你刚刚及格了";
elseif ($achievement>=70 && $achievement
echo "你得了良好";
elseif ($achievement>=80 && $achievement
echo "你很优秀哦!";
else
echo "你兼职太棒了!"
?>
switch (date("D")) {
case "Mon":
echo "今天星期一";
break;
case "Tue":
echo "今天星期二";
break;
case "Wed":
echo "今天星期三";
break;
case "Thu":
echo "今天星期四";
break;
case "Fri":
echo "今天星期五";
break;
default:
echo "今天放假";
break;
}
?>
2.5.2 循环语句
/* 应用,每个条件都有*/
for ($i = 1; $i
print $i. "-";
/* 应用,省略第个表达式*/
print "
";
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
print $i. "-";
}
print "
";
/* 应用,省略个表达式*/
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
print $i. "-";
$i++;
}
print "
";
/* 应用*/
for ($i = 1; $i
print "
";
/* 应用 */
for ($i = 1; $i
?>
/* 应用*/
$i = 1;
while ($i
print $i++ . "-";
}
print "
";
/* 应用*/
$i = 1;
while ($i
print $i . "-";
$i++;
endwhile;
print "
";
/* 应用*/
$i = 1;
while ($i
print $i . "-";
$i++;
if ($i>10) break;
endwhile;
?>
2.5.3 break和continue语句
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5
\n";
break 1; /* 只跳出switch循环*/
case 10:
echo "At 10; quitting
\n";
break 2; /* 跳出switch和while循环*/
default:
break;
}
}
?>
$i=0;
while ($i++
if ($i==2) { // 跳出,也就是不会输出i am 2
continue;
}
echo "i am $i
";
}
$i = 0;
while ($i++
echo "外层
\n";
while (1) {
echo " 中间层
\n";
while (1) {
echo " 内层
\n";
$i=6;
continue 3;
}
//因为每次到内层的时候,就跳到第一层,不会被执行
echo "我永远不会被输出的~~.
\n";
}
echo "我也是不会被输出的~~.
\n";
}
?>
2.6 数组
2.6.1 数组类型
枚举数组
关联数组
多维数组
$student=array("0"=>array("name"=>"James","sex"=>"male","age"=>"28"),
"1"=>array("name"=>"John","sex"=>"male","age"=>"25"),
"2"=>array("name"=>"Susan","sex"=>"female","age"=>"24"));
Print $student[2][age]
?>
2.6.2 数组初始化
使用数组标识符
2.6.3 数组的应用
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);
echo "
";
// 现在删除其中的所有单元,但保持数组本身的结构
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
echo "
";
// 添加一个单元(注意新的键名是5,而不是你可能以为的0)
$array[] = 6;
print_r($array);
// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
2.7 函数
2.7.1 内置函数
2.7.2 自定义函数
2.7.3 函数变量的作用域
2.8 日期和时间处理
2.8.1 获取日期和时间
2.8.2 使用getdate函数获得日期信息
2.8.3 使用mktime函数取得一个日期的时间戳

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
