1、基本语法
PHP 脚本可放置于文档中的任何位置。
PHP 脚本以 结尾
<?php// 此处是 PHP 代码?>
PHP 文件的默认文件扩展名是 ".php"。
PHP 文件通常包含 HTML 标签以及一些 PHP 脚本代码。
下面的例子是一个简单的 PHP 文件,其中包含了使用内建 PHP 函数 "echo" 在网页上输出文本 "Hello World!" 的一段 PHP 脚本
<!DOCTYPE html><html><body><h1 id="我的第一张-PHP-页面">我的第一张 PHP 页面</h1><?phpecho "Hello World!";?> </body></html>
2、php注释
<!DOCTYPE html><html><body> <?php// 这是单行注释# 这也是单行注释/*这是多行注释块它横跨了多行*/echo "Hello World!";?> </body></html>
3、php是大小写 不敏感的
<!DOCTYPE html><html><body><!--大小写 不敏感--><?phpECHO "Hello World!<br>";echo "Hello World!<br>";EcHo "Hello World!<br>";?> </body></html>
4、php变量
变量视为存储数据的容器,
PHP 是一门类型松散的语言
在上面的例子中,请注意我们不必告知 PHP 变量的数据类型。
PHP 根据它的值,自动把变量转换为正确的数据类型
PHP 变量规则:
注释:PHP 变量名称对大小写敏感!
PHP 有三种不同的变量作用域:
!doctype html><html><body><?php$x=5;$y=6;$z=$x+$y;echo $z;?></body></html>
<!DOCTYPE html><html><body><?php$x=5; // global scope function myTest() { $y=10; // local scope echo "<p>在函数内部测试变量:</p>"; echo "变量 x 是:$x"; echo "<br>"; echo "变量 y 是:$y";} myTest();echo "<p>在函数之外测试变量:</p>";echo "变量 x 是:$x";echo "<br>";echo "变量 y 是:$y";?></body></html>
<!DOCTYPE html><html><body><?php$x=5;$y=10;function myTest() { global $x,$y; $y=$x+$y;} myTest(); // 运行函数echo $y; // 输出变量 $y 的新值 15?></body></html>
以上代码输出结果为15(演示global变量使用方法)
<!DOCTYPE html><html><body><?phpfunction myTest() { static $x=0; echo $x; $x++;}myTest();echo "<br>";myTest();echo "<br>";myTest();echo "<br>";myTest();echo "<br>";myTest();?> </body></html>
以上代码输出结果为0,1,2,3,4,5,(演示static静态变量用,然后,每当函数被调用时,这个变量所存储的信息都是函数最后一次被调用时所包含的信息,注释:该变量仍然是函数的局部变量)
5、echo和print语句
echo 和 print 之间的差异:
提示:echo 比 print 稍快,因为它不返回任何值。
echo 、print都是一个语言结构,有无括号均可使用:echo 或 echo()、:print 或 print()。
<!DOCTYPE html><html><body><?phpecho "<h2 id="PHP-很有趣">PHP 很有趣!</h2>";echo "Hello world!<br>";echo "我计划学习 PHP!<br>";echo "这段话", "由", "多个", "字符串", "串接而成。";$txt1="Learn PHP";$txt2="W3School.com.cn";$cars=array("Volvo","BMW","SAAB");echo $txt1;echo "<br>";echo "Study PHP at $txt2";echo "<br>";echo "My car is a {$cars[0]}";?></body></html>
<!DOCTYPE html><html><body><?phpprint "<h2 id="PHP-is-fun">PHP is fun!</h2>";print "Hello world!<br>";print "I'm about to learn PHP!";$txt1="Learn PHP";$txt2="W3School.com.cn";$cars=array("Volvo","BMW","SAAB");print $txt1;print "<br>";print "Study PHP at $txt2";print "<br>";print "My car is a {$cars[0]}";?> </body></html>
6、php数据类型
字符串、整数、浮点数、逻辑、数组、对象、NULL。
<!DOCTYPE html><html><body><?php #字符串$x = "Hello world!";echo $x;echo "<br>"; $x = 'Hello world!';echo $x;//整数<?php $x = 5985;var_dump($x);echo "<br>"; $x = -345; // 负数 var_dump($x);echo "<br>"; $x = 0x8C; // 十六进制数var_dump($x);echo "<br>";$x = 047; // 八进制数var_dump($x);//浮点数$x = 10.365;var_dump($x);echo "<br>"; $x = 2.4e3;var_dump($x);echo "<br>"; $x = 8E-5;var_dump($x);//boolean$x=true;$y=false;//数组$cars=array("Volvo","BMW","SAAB");var_dump($cars);//对象class Car{ var $color; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; }}//打印对象中的变量function print_vars($obj) { foreach (get_object_vars($obj) as $prop => $val) { echo "\t$prop = $val\n"; }}//对对象进行初始化$herbie = new Car("white");// show herbie propertiesecho "\herbie: Properties\n";print_vars($herbie);/*特殊的 NULL 值表示变量无值。NULL 是数据类型 NULL 唯一可能的值。NULL 值标示变量是否为空。也用于区分空字符串与空值数据库。可以通过把值设置为 NULL,将变量清空*/$x="Hello world!";$x=null;var_dump($x)?></body></html>
7、php字符串函数
strlen("Hello world!");
以上代码的输出是:12
提示:strlen() 常用于循环和其他函数,在确定字符串何时结束很重要时。(例如,在循环中,我们也许需要在字符串的最后一个字符之后停止循环)。、
<?phpecho strpos("Hello world!","world");?>
以上代码的输出是:6。
提示:上例中字符串 "world" 的位置是 6。是 6(而不是 7)的理由是,字符串中首字符的位置是 0 而不是 1。
8、php常量
常量类似变量,但是常量一旦被定义就无法更改或撤销定义。
如需设置常量,请使用 define() 函数 - 它使用三个参数:
- 首个参数定义常量的名称
- 第二个参数定义常量的值
- 可选的第三个参数规定常量名是否对大小写敏感。默认是 false。
下例创建了一个对大小写敏感的常量,值为 "Welcome to W3School.com.cn!":
define("GREETING", "Welcome to W3School.com.cn!");echo GREETING;
下例创建了一个对大小写不敏感的常量,值为 "Welcome to W3School.com.cn!":
define("GREETING", "Welcome to W3School.com.cn!", true);echo greeting;
9、运算符
①算数运算符
+ | 加法 | $x + $y | $x 与 $y 求和 |
- | 减法 | $x - $y | $x 与 $y 的差数 |
* | 乘法 | $x * $y | $x 与 $y 的乘积 |
/ | 除法 | $x / $y | $x 与 $y 的商数 |
% | 模数 | $x % $y | $x 除 $y 的余数 |
$x=10; $y=6;echo ($x + $y); // 输出 16echo ($x - $y); // 输出 4echo ($x * $y); // 输出 60echo ($x / $y); // 输出 1.6666666666667echo ($x % $y); // 输出 4
②值运算符
x = y | x = y | 右侧表达式为左侧运算数设置值。 |
x += y | x = x + y | 加 |
x -= y | x = x - y | 减 |
x *= y | x = x * y | 乘 |
x /= y | x = x / y | 除 |
x %= y | x = x % y | 模数 |
<?php $x=10; echo $x; // 输出 10$y=20; $y += 100;echo $y; // 输出 120$z=50;$z -= 25;echo $z; // 输出 25$i=5;$i *= 6;echo $i; // 输出 30$j=10;$j /= 5;echo $j; // 输出 2$k=15;$k %= 4;echo $k; // 输出 3?>
③字符串运算符
. | 串接 | $txt1 = "Hello" $txt2 = $txt1 . " world!" | 现在 $txt2 包含 "Hello world!" |
.= | 串接赋值 | $txt1 = "Hello" $txt1 .= " world!" | 现在 $txt1 包含 "Hello world!" |
<?php$a = "Hello";$b = $a . " world!";echo $b; // 输出 Hello world!$x="Hello";$x .= " world!";echo $x; // 输出 Hello world!?>
④递增递减运算符
++$x | 前递增 | $x 加一递增,然后返回 $x |
$x++ | 后递增 | 返回 $x,然后 $x 加一递增 |
--$x | 前递减 | $x 减一递减,然后返回 $x |
$x-- | 后递减 | 返回 $x,然后 $x 减一递减 |
<?php$x=10; echo ++$x; // 输出 11$y=10; echo $y++; // 输出 10$z=5;echo --$z; // 输出 4$i=5;echo $i--; // 输出 5?>
⑤比较运算符
== | 等于 | $x == $y | 如果 $x 等于 $y,则返回 true。 |
=== | 全等(完全相同) | $x === $y | 如果 $x 等于 $y,且它们类型相同,则返回 true。 |
!= | 不等于 | $x != $y | 如果 $x 不等于 $y,则返回 true。 |
不等于 | $x $y | 如果 $x 不等于 $y,则返回 true。 | |
!== | 不全等(完全不同) | $x !== $y | 如果 $x 不等于 $y,且它们类型不相同,则返回 true。 |
> | 大于 | $x > $y | 如果 $x 大于 $y,则返回 true。 |
大于 | $x | 如果 $x 小于 $y,则返回 true。 | |
>= | 大于或等于 | $x >= $y | 如果 $x 大于或者等于 $y,则返回 true. |
小于或等于 | $x | 如果 $x 小于或者等于 $y,则返回 true。 |
<?php$x=100; $y="100";var_dump($x == $y);echo "<br>";var_dump($x === $y);echo "<br>";var_dump($x != $y);echo "<br>";var_dump($x !== $y);echo "<br>";$a=50;$b=90;var_dump($a > $b);echo "<br>";var_dump($a < $b);?>
⑥逻辑运算符
and | 与 | $x and $y | 如果 $x 和 $y 都为 true,则返回 true。 |
or | 或 | $x or $y | 如果 $x 和 $y 至少有一个为 true,则返回 true。 |
xor | 异或 | $x xor $y | 如果 $x 和 $y 有且仅有一个为 true,则返回 true。 |
&& | 与 | $x && $y | 如果 $x 和 $y 都为 true,则返回 true。 |
|| | 或 | $x || $y | 如果 $x 和 $y 至少有一个为 true,则返回 true。 |
! | 非 | !$x | 如果 $x 不为 true,则返回 true。 |
⑦数组运算符
+ | 联合 | $x + $y | $x 和 $y 的联合(但不覆盖重复的键) |
== | 相等 | $x == $y | 如果 $x 和 $y 拥有相同的键/值对,则返回 true。 |
=== | 全等 | $x === $y | 如果 $x 和 $y 拥有相同的键/值对,且顺序相同类型相同,则返回 true。 |
!= | 不相等 | $x != $y | 如果 $x 不等于 $y,则返回 true。 |
不相等 | $x $y | 如果 $x 不等于 $y,则返回 true。 | |
!== | 不全等 | $x !== $y | 如果 $x 与 $y 完全不同,则返回 true。 |
<?php$x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // $x 与 $y 的联合var_dump($z);var_dump($x == $y);var_dump($x === $y);var_dump($x != $y);var_dump($x <> $y);var_dump($x !== $y);?>
10、控制结构
if else
<?php$t=date("H");if ($t<"10") { echo "Have a good morning!";} elseif ($t<"20") { echo "Have a good day!";} else { echo "Have a good night!";}?>
switch
<?phpswitch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?></body></html>
While
<?php $x=1; while($x<=5) { echo "这个数字是:$x <br>"; $x++;} ?>
Do While循环
<?php $x=1; do { echo "这个数字是:$x <br>"; $x++;} while ($x<=5);?>
for循环
<?php for ($x=0; $x<=10; $x++) { echo "数字是:$x <br>";} ?>
foreach循环
foreach 循环只适用于数组,并用于遍历数组中的每个键/值对
foreach ($array as $value) { code to be executed;}<br />每进行一次循环迭代,当前数组元素的值就会被赋值给 $value 变量,并且数组指针会逐一地移动,直到到达最后一个数组元素。
<?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) { echo "$value <br>";}?>
11、函数
不带参数的函数
<?phpfunction writeMsg() { echo "Hello world!";}writeMsg(); // 调用函数?>
带参数的函数
<?phpfunction familyName($fname) { echo "$fname Zhang.<br>";}familyName("Li");familyName("Hong");familyName("Tao");familyName("Xiao Mei");familyName("Jian");?>
还有一种情况,是默认的参数:
<?phpfunction setHeight($minheight=50) { echo "The height is : $minheight <br>";}setHeight(350);setHeight(); // 将使用默认值 50setHeight(135);setHeight(80);?>
函数返回值:
<?phpfunction sum($x,$y) { $z=$x+$y; return $z;}echo "5 + 10 = " . sum(5,10) . "<br>";echo "7 + 13 = " . sum(7,13) . "<br>";echo "2 + 4 = " . sum(2,4);?>
12、数组
数组能够在单独的变量名中存储一个或多个值。
<?php$cars=array("Volvo","BMW","SAAB");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?>
在 PHP 中,有三种数组类型:
①索引数组
索引是自动分配的(索引从 0 开始):
$cars=array("Volvo","BMW","SAAB");
或者也可以手动分配索引:
$cars[0]="Volvo";$cars[1]="BMW";$cars[2]="SAAB";<br /><br /><br />获取数组长度
遍历数组
";}?>
②关联数组
关联数组是使用您分配给数组的指定键的数组。
有两种创建关联数组的方法:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
或者:
$age['Peter']="35";$age['Ben']="37";$age['Joe']="43";
<?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");echo "Peter is " . $age['Peter'] . " years old.";?>
遍历:
<?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>";}?>
③多维数组
13、排序
14、超全局变量
超全局变量 在 PHP 4.1.0 中引入,是在全部作用域中始终可用的内置变量。
PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。在函数或方法中无需执行 global $variable; 就可以访问它们。
这些超全局变量是:

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools