Table of Contents
我的第一张 PHP 页面
PHP 是一门类型松散的语言
PHP 变量规则:
PHP 很有趣!
PHP is fun!
Home Backend Development PHP Tutorial 23、php知识点总结基础教程part-1

23、php知识点总结基础教程part-1

Jun 23, 2016 pm 01:10 PM

1、基本语法

PHP 脚本可放置于文档中的任何位置。

PHP 脚本以  结尾

<?php// 此处是 PHP 代码?>
Copy after login

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>
Copy after login

2、php注释

<!DOCTYPE html><html><body>  <?php// 这是单行注释# 这也是单行注释/*这是多行注释块它横跨了多行*/echo "Hello World!";?>  </body></html>
Copy after login

3、php是大小写 不敏感的

<!DOCTYPE html><html><body><!--大小写 不敏感--><?phpECHO "Hello World!<br>";echo "Hello World!<br>";EcHo "Hello World!<br>";?>  </body></html>
Copy after login

4、php变量

变量视为存储数据的容器,

PHP 是一门类型松散的语言

在上面的例子中,请注意我们不必告知 PHP 变量的数据类型。

PHP 根据它的值,自动把变量转换为正确的数据类型

PHP 变量规则:

  • 变量以 $ 符号开头,其后是变量的名称
  • 变量名称必须以字母或下划线开头
  • 变量名称不能以数字开头
  • 变量名称只能包含字母数字字符和下划线(A-z、0-9 以及 _)
  • 变量名称对大小写敏感($y 与 $Y 是两个不同的变量)
  • 注释:PHP 变量名称对大小写敏感!

    PHP 有三种不同的变量作用域:

  • local(局部)  函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。
  • global(全局) 函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。global 关键词用于访问函数内的全局变量,要做到这一点,请在(函数内部)变量前面使用 global 关键词
  • static(静态)
  • !doctype html><html><body><?php$x=5;$y=6;$z=$x+$y;echo $z;?></body></html>
    Copy after login

    <!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>
    Copy after login

    <!DOCTYPE html><html><body><?php$x=5;$y=10;function myTest() {   global $x,$y;   $y=$x+$y;} myTest(); // 运行函数echo $y; // 输出变量 $y 的新值 15?></body></html>
    Copy after login

    以上代码输出结果为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>
    Copy after login

    以上代码输出结果为0,1,2,3,4,5,(演示static静态变量用,然后,每当函数被调用时,这个变量所存储的信息都是函数最后一次被调用时所包含的信息,注释:该变量仍然是函数的局部变量)

    5、echo和print语句

    echo 和 print 之间的差异:

  • echo - 能够输出一个以上的字符串
  • print - 只能输出一个字符串,并始终返回 1
  • 提示: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>
    Copy after login

    <!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>
    Copy after login

    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>
    Copy after login

    7、php字符串函数

    strlen("Hello world!");
    Copy after login

    以上代码的输出是:12

    提示:strlen() 常用于循环和其他函数,在确定字符串何时结束很重要时。(例如,在循环中,我们也许需要在字符串的最后一个字符之后停止循环)。、

    <?phpecho strpos("Hello world!","world");?>
    Copy after login

    以上代码的输出是:6。

    提示:上例中字符串 "world" 的位置是 6。是 6(而不是 7)的理由是,字符串中首字符的位置是 0 而不是 1。

    8、php常量

    常量类似变量,但是常量一旦被定义就无法更改或撤销定义。

    如需设置常量,请使用 define() 函数 - 它使用三个参数:

    1. 首个参数定义常量的名称
    2. 第二个参数定义常量的值
    3. 可选的第三个参数规定常量名是否对大小写敏感。默认是 false。

    下例创建了一个对大小写敏感的常量,值为 "Welcome to W3School.com.cn!":

    define("GREETING", "Welcome to W3School.com.cn!");echo GREETING;
    Copy after login

    下例创建了一个对大小写不敏感的常量,值为 "Welcome to W3School.com.cn!":

    define("GREETING", "Welcome to W3School.com.cn!", true);echo greeting;
    Copy after login

    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
    Copy after login

    ②值运算符

    赋值 等同于 描述
    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?>
    Copy after login

    ③字符串运算符

    运算符 名称 例子 结果
    . 串接 $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!?>
    Copy after login

    ④递增递减运算符

    运算符 名称 描述
    ++$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?>
    Copy after login

    ⑤比较运算符

    运算符 名称 例子 结果
    == 等于 $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);?>
    Copy after login

    ⑥逻辑运算符

    运算符 名称 例子 结果
    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);?>
    Copy after login

    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!";}?>
    Copy after login

    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>
    Copy after login

    While

    <?php $x=1; while($x<=5) {  echo "这个数字是:$x <br>";  $x++;} ?>
    Copy after login

    Do While循环

    <?php $x=1; do {  echo "这个数字是:$x <br>";  $x++;} while ($x<=5);?>
    Copy after login

    for循环

    <?php for ($x=0; $x<=10; $x++) {  echo "数字是:$x <br>";} ?>
    Copy after login

    foreach循环

    foreach 循环只适用于数组,并用于遍历数组中的每个键/值对

    foreach ($array as $value) {  code to be executed;}<br />每进行一次循环迭代,当前数组元素的值就会被赋值给 $value 变量,并且数组指针会逐一地移动,直到到达最后一个数组元素。
    Copy after login

    <?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) {  echo "$value <br>";}?>
    Copy after login

    11、函数

    不带参数的函数

    <?phpfunction writeMsg() {  echo "Hello world!";}writeMsg(); // 调用函数?>
    Copy after login

    带参数的函数

    <?phpfunction familyName($fname) {  echo "$fname Zhang.<br>";}familyName("Li");familyName("Hong");familyName("Tao");familyName("Xiao Mei");familyName("Jian");?>
    Copy after login

    还有一种情况,是默认的参数:

    <?phpfunction setHeight($minheight=50) {  echo "The height is : $minheight <br>";}setHeight(350);setHeight(); // 将使用默认值 50setHeight(135);setHeight(80);?>
    Copy after login

    函数返回值:

    <?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);?>
    Copy after login

    12、数组

    数组能够在单独的变量名中存储一个或多个值。

    <?php$cars=array("Volvo","BMW","SAAB");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?>
    Copy after login

    在 PHP 中,有三种数组类型:

  • 索引数组 - 带有数字索引的数组
  • 关联数组 - 带有指定键的数组
  • 多维数组 - 包含一个或多个数组的数组
  • ①索引数组

    索引是自动分配的(索引从 0 开始):

    $cars=array("Volvo","BMW","SAAB");
    Copy after login

    或者也可以手动分配索引:

    $cars[0]="Volvo";$cars[1]="BMW";$cars[2]="SAAB";<br /><br /><br />获取数组长度
    Copy after login

    Copy after login

    遍历数组

    ";}?>
    Copy after login

    ②关联数组

    关联数组是使用您分配给数组的指定键的数组。

    有两种创建关联数组的方法:

    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
    Copy after login

    或者:

    $age['Peter']="35";$age['Ben']="37";$age['Joe']="43";
    Copy after login

    <?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");echo "Peter is " . $age['Peter'] . " years old.";?>
    Copy after login

    遍历:

    <?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");foreach($age as $x=>$x_value) {  echo "Key=" . $x . ", Value=" . $x_value;  echo "<br>";}?>
    Copy after login

     

    ③多维数组

     

    13、排序

  • sort() - 以升序对数组排序
  • rsort() - 以降序对数组排序
  • asort() - 根据值,以升序对关联数组进行排序
  • ksort() - 根据键,以升序对关联数组进行排序
  • arsort() - 根据值,以降序对关联数组进行排序
  • krsort() - 根据键,以降序对关联数组进行排序
  •  

    14、超全局变量

    超全局变量 在 PHP 4.1.0 中引入,是在全部作用域中始终可用的内置变量。

    PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。在函数或方法中无需执行 global $variable; 就可以访问它们。

    这些超全局变量是:

  • $GLOBALS  引用全局作用域中可用的全部变量
  • $_SERVER 这种超全局变量保存关于报头、路径和脚本位置的信息。
  • $_REQUEST  用于收集 HTML 表单提交的数据。
  • $_POST 广泛用于收集提交 method="post" 的 HTML 表单后的表单数据。$_POST 也常用于传递变量
  • $_GET 可用于收集提交 HTML 表单 (method="get") 之后的表单数据。
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION
  •  

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

    Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

    Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

    The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

    The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

    What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

    An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

    See all articles