23、php知识点总结基础教程part-1
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 < $y | 如果 $x 小于 $y,则返回 true。 |
>= | 大于或等于 | $x >= $y | 如果 $x 大于或者等于 $y,则返回 true. |
<= | 小于或等于 | $x <= $y | 如果 $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; 就可以访问它们。
这些超全局变量是:

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Laravel simplifie la gestion des données de session temporaires à l'aide de ses méthodes de flash intuitives. Ceci est parfait pour afficher de brefs messages, alertes ou notifications dans votre application. Les données ne persistent que pour la demande ultérieure par défaut: $ demande-

L'extension PHP Client URL (CURL) est un outil puissant pour les développeurs, permettant une interaction transparente avec des serveurs distants et des API REST. En tirant parti de Libcurl, une bibliothèque de transfert de fichiers multi-protocol très respectée, PHP Curl facilite Efficient Execu

Laravel fournit une syntaxe de simulation de réponse HTTP concise, simplifiant les tests d'interaction HTTP. Cette approche réduit considérablement la redondance du code tout en rendant votre simulation de test plus intuitive. L'implémentation de base fournit une variété de raccourcis de type de réponse: Utiliser illuminate \ support \ faades \ http; Http :: faux ([[ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Alipay Php ...

Voulez-vous fournir des solutions instantanées en temps réel aux problèmes les plus pressants de vos clients? Le chat en direct vous permet d'avoir des conversations en temps réel avec les clients et de résoudre leurs problèmes instantanément. Il vous permet de fournir un service plus rapide à votre personnalité

L'article traite de la liaison statique tardive (LSB) dans PHP, introduite dans PHP 5.3, permettant une résolution d'exécution de la méthode statique nécessite un héritage plus flexible. Problème main: LSB vs polymorphisme traditionnel; Applications pratiques de LSB et perfo potentiel

L'article examine l'ajout de fonctionnalités personnalisées aux cadres, en se concentrant sur la compréhension de l'architecture, l'identification des points d'extension et les meilleures pratiques pour l'intégration et le débogage.

L'article traite des fonctionnalités de sécurité essentielles dans les cadres pour se protéger contre les vulnérabilités, notamment la validation des entrées, l'authentification et les mises à jour régulières.
