search
HomeBackend DevelopmentPHP TutorialBasic introductory knowledge of Php

Basic introductory knowledge of Php

Mar 10, 2018 am 11:10 AM
phpgetting StartedKnowledge

This PHP introductory tutorial guides beginners to quickly introduce PHP knowledge. Although it is very simple, it is very comprehensive. I hope it can help everyone.

1. Introduction to PHP

PHP scripts are executed on the server.

Before learning PHP, you need to have a basic understanding of the following knowledge:

  • ##HTML

  • CSS

  • JavaScript

1.1 What is PHP?

  • PHP is an acronym for "PHP Hypertext Preprocessor"

  • PHP Is a widely used open source scripting language

  • PHP scripts are executed on the server

  • PHP has no cost and is free to download and use

1.2 PHP is an amazingly popular language!

  • #It is powerful enough to become the core of the largest blogging system on the Internet (WordPress)!

  • It's deep enough to run the largest social network (facebook)!

  • #And it’s easy to use enough to become the preferred server-side language for beginners!

#1.3 What is a PHP file?

  • PHP files can contain text, HTML, CSS, and PHP code

  • PHP code Executed on the server, and the results are returned to the browser in plain text

  • The suffix of the PHP file is ".php"

1.4 What can PHP do?

  • PHP can generate dynamic page content

  • PHP can create, open, and read , write, delete and close files on the server

  • PHP can receive form data

  • PHP can send and retrieve cookies

  • PHP can add, delete, and modify data in the database

  • PHP can restrict users from accessing certain pages in the website

  • PHP can encrypt data

Through PHP, you can not be limited to only outputting HTML. You can also export images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

1.5 Why use PHP?

  • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

  • PHP is compatible with almost all servers (Apache, IIS, etc.)

  • PHP supports multiple databases

  • PHP is free.

  • PHP is easy to learn and runs efficiently on the server side

1.6 PHP Installation

The official PHP website (PHP.net) provides PHP installation instructions: http://php.net/manual/zh/install.php

2. PHP syntax

The PHP script is executed on the server and then sends back pure HTML results to the browser.

2.1 Basic PHP syntax

PHP scripts can be placed anywhere in the document.

PHP scripts start with :

<span style="font-size: 14px;"><?php // 此处是 PHP 代码?><br></span>

The default file extension for PHP files is ". php".

PHP files usually contain HTML tags as well as some PHP script code.

The following example is a simple PHP file, which contains a PHP script that uses the built-in PHP function "echo" to output the text "Hello World!" on a web page:

Example
<span style="font-size: 14px;"><!DOCTYPE html><br/><html><br/><body><br/><br/><h1>我的第一张 PHP 页面</h1><br/><br/><?php<br/>echo "Hello World!";<br/>?><br/><br/></body><br/></html><br/></span>

运行结果:

我的第一张 PHP 页面

Hello World!

注释:PHP 语句以分号结尾(;)。PHP 代码块的关闭标签也会自动表明分号(因此在 PHP 代码块的最后一行不必使用分号)。

2.2 PHP 中的注释

PHP 代码中的注释不会被作为程序来读取和执行。它唯一的作用是供代码编辑者阅读。

注释用于:

  • 使其他人理解您正在做的工作 - 注释可以让其他程序员了解您在每个步骤进行的工作(如果您供职于团队)

  • 提醒自己做过什么 - 大多数程序员都曾经历过一两年后对项目进行返工,然后不得不重新考虑他们做过的事情。注释可以记录您在写代码时的思路。

PHP 支持三种注释:

实例

<span style="font-size: 14px;"><!DOCTYPE html><br/><html><br/><body><br/><br/><?php<br/>// 这是单行注释<br/><br/># 这也是单行注释<br/><br/>/*<br/>这是多行注释块<br/>它横跨了<br/>多行<br/>*/<br/>?><br/><br/></body><br/></html></span>

2.3 PHP 大小写敏感

在 PHP 中,所有用户定义的函数、类和关键词(例如 if、else、echo 等等)都对大小写不敏感。

在下面的例子中,所有这三天 echo 语句都是合法的(等价):

实例

<span style="font-size: 14px;"><!DOCTYPE html><br/><html><br/><body><br/><br/><?php<br/>ECHO "Hello World!<br>";<br/>echo "Hello World!<br>";<br/>EcHo "Hello World!<br>";<br/>?><br/><br/></body><br/></html></span>

运行结果:

Hello World!
Hello World!
Hello World!

不过在 PHP 中,所有变量都对大小写敏感。

在下面的例子中,只有第一条语句会显示 $color 变量的值(这是因为 $color、$COLOR 以及 $coLOR 被视作三个不同的变量):

实例

<span style="font-size: 14px;"><!DOCTYPE html><br/><html><br/><body><br/><br/><?php<br/>$color="red";<br/>echo "My car is " . $color . "<br>";<br/>echo "My house is " . $COLOR . "<br>";<br/>echo "My boat is " . $coLOR . "<br>";<br/>?><br/><br/></body><br/></html><br/></span>

运行结果

My car is red
My house is
My boat is

3、PHP 变量

变量是存储信息的容器:

实例

<span style="font-size: 14px;"><?php<br/>$x=5;<br/>$y=6;<br/>$z=$x+$y;<br/>echo $z;<br/>?><br/></span>

运行结果:

11

3.1 类似代数

<span style="font-size: 14px;">x=5<br>y=6<br>z=x+y</span>

在代数中我们使用字母(比如 x)来保存值(比如 5)。

从上面的表达式 z=x+y,我们能够计算出 z 的值是 11。

在 PHP 中,这三个字母被称为变量。

注释:请把变量视为存储数据的容器。

3.2 PHP 变量

正如代数,PHP 变量可用于保存值(x=5)和表达式(z=x+y)。

变量的名称可以很短(比如 x 和 y),也可以取更具描述性的名称(比如 carname、total_volume)。

PHP 变量规则:

  • 变量以 $ 符号开头,其后是变量的名称

  • 变量名称必须以字母或下划线开头

  • 变量名称不能以数字开头

  • 变量名称只能包含字母数字字符和下划线(A-z、0-9 以及 _)

  • 变量名称对大小写敏感($y 与 $Y 是两个不同的变量)

注释:PHP 变量名称对大小写敏感!

3.3 创建 PHP 变量

PHP 没有创建变量的命令。

变量会在首次为其赋值时被创建:

实例

<span style="font-size: 14px;"><?php<br/>$txt="Hello world!";<br/>$x=5;<br/>$y=10.5;<br/>?></span>

以上语句执行后,变量 txt 会保存值 Hello world!,变量 x 会保存值 5,变量 y 会保存值 10.5。

注释:如果您为变量赋的值是文本,请用引号包围该值。

3.4 PHP 是一门类型松散的语言

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

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

在诸如 C 和 C++ 以及 Java 之类的语言中,程序员必须在使用变量之前声明它的名称和类型。

3.5 PHP 变量作用域

在 PHP 中,可以在脚本的任意位置对变量进行声明。

变量的作用域指的是变量能够被引用/使用的那部分脚本。

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

  • local(局部)

  • global(全局)

  • static(静态)

3.5.1 Local 和 Global 作用域

函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。

函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。

下面的例子测试了带有局部和全局作用域的变量:

实例

<span style="font-size: 14px;"><?php<br/>$x=5; // 全局作用域function myTest() {<br/>  $y=10; // 局部作用域<br/>  echo "<p>测试函数内部的变量:</p>";<br/>  echo "变量 x 是:$x";<br/>  echo "<br>";<br/>  echo "变量 y 是:$x";<br/>} <br/><br/>myTest();<br/><br/>echo "<p>测试函数之外的变量:</p>";<br/>echo "变量 x 是:$x";<br/>echo "<br>";<br/>echo "变量 y 是:$x";<br/>?></span>

在上例中,有两个变量 $x 和 $y,以及一个函数 myTest()。$x 是全局变量,因为它是在函数之外声明的,而 $y 是局部变量,因为它是在函数内声明的。

如果我们在 myTest() 函数内部输出两个变量的值,$y 会输出在本地声明的值,但是无法 $x 的值,因为它在函数之外创建。

然后,如果在 myTest() 函数之外输出两个变量的值,那么会输出 $x 的值,但是不会输出 $y 的值,因为它是局部变量,并且在 myTest() 内部创建。

注释:您可以在不同的函数中创建名称相同的局部变量,因为局部变量只能被在其中创建它的函数识别。

3.5.2 PHP global 关键词

global 关键词用于访问函数内的全局变量。

要做到这一点,请在(函数内部)变量前面使用 global 关键词:

实例

<span style="font-size: 14px;"><?php<br/>$x=5;<br/>$y=10;<br/><br/>function myTest() {<br/>  global $x,$y;<br/>  $y=$x+$y;<br/>}<br/><br/>myTest();<br/>echo $y; // 输出 15?><br/></span>

 

PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。下标存有变量名。这个数组在函数内也可以访问,并能够用于直接更新全局变量。

上面的例子可以这样重写:

实例

<span style="font-size: 14px;"><?php<br/>$x=5;<br/>$y=10;<br/><br/>function myTest() {<br/>  $GLOBALS[&#39;y&#39;]=$GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];<br/>} <br/><br/>myTest();<br/>echo $y; // 输出 15?></span>

3.5.3 PHP static 关键词

通常,当函数完成/执行后,会删除所有变量。不过,有时我需要不删除某个局部变量。实现这一点需要更进一步的工作。

要完成这一点,请在您首次声明变量时使用 static 关键词:

实例

<span style="font-size: 14px;"><?php<br/><br/>function myTest() {<br/>  static $x=0;<br/>  echo $x;<br/>  $x++;<br/>}<br/><br/>myTest();<br/>myTest();<br/>myTest();<br/><br/>?><br/></span>

然后,每当函数被调用时,这个变量所存储的信息都是函数最后一次被调用时所包含的信息。

注释:该变量仍然是函数的局部变量。

 

4、PHP 5 echo 和 print 语句

 

在 PHP 中,有两种基本的输出方法:echo 和 print。

4.1 PHP echo 和 print 语句差异

echo 和 print 之间的差异:

  • echo - 能够输出一个以上的字符串

  • print - 只能输出一个字符串,并始终返回 1

提示:echo 比 print 稍快,因为它不返回任何值。

4.2 PHP echo 语句

echo 是一个语言结构,有无括号均可使用:echo 或 echo()。

显示字符串

下面的例子展示如何用 echo 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):

<span style="font-size: 14px;"><?php<br/>echo "<h2 id="PHP-is-fun">PHP is fun!</h2>";<br/>echo "Hello world!<br>";<br/>echo "I&#39;m about to learn PHP!<br>";<br/>echo "This", " string", " was", " made", " with multiple parameters.";<br/>?><br/></span>

显示变量

下面的例子展示如何用 echo 命令来显示字符串和变量:

<span style="font-size: 14px;"><?php<br/>$txt1="Learn PHP";<br/>$txt2="W3School.com.cn";<br/>$cars=array("Volvo","BMW","SAAB");<br/><br/>echo $txt1;<br/>echo "<br>";<br/>echo "Study PHP at $txt2";<br/>echo "My car is a {$cars[0]}";<br/>?><br/></span>

运行结果 

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

4.3 PHP print 语句

print 也是语言结构,有无括号均可使用:print 或 print()。

显示字符串

下面的例子展示如何用 print 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):

<span style="font-size: 14px;"><?php<br/>print "<h2 id="PHP-is-fun">PHP is fun!</h2>";<br/>print "Hello world!<br>";<br/>print "I&#39;m about to learn PHP!";<br/>?><br/></span>

运行结果:

PHP is fun!

Hello world!
I'm about to learn PHP!

显示变量

下面的例子展示如何用 print 命令来显示字符串和变量:

<span style="font-size: 14px;"><?php<br/>$txt1="Learn PHP";<br/>$txt2="W3School.com.cn";<br/>$cars=array("Volvo","BMW","SAAB");<br/><br/>print $txt1;<br/>print "<br>";<br/>print "Study PHP at $txt2";<br/>print "My car is a {$cars[0]}";<br/>?><br/></span>

运行结果:

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

 

5、PHP 数据类型

字符串、整数、浮点数、逻辑、数组、对象、NULL。

5.1 PHP 字符串

字符串是字符序列,比如 "Hello world!"。

字符串可以是引号内的任何文本。您可以使用单引号或双引号:

实例

<span style="font-size: 14px;"><?php <br/>$x = "Hello world!";<br/>echo $x;<br/>echo "<br>"; <br/>$x = &#39;Hello world!&#39;;<br/>echo $x;<br/>?><br/></span>

 

5.2 PHP 整数

整数是没有小数的数字。

整数规则:

  • 整数必须有至少一个数字(0-9)

  • 整数不能包含逗号或空格

  • 整数不能有小数点

  • 整数正负均可

  • 可以用三种格式规定整数:十进制、十六进制(前缀是 0x)或八进制(前缀是 0)

在下面的例子中,我们将测试不同的数字。PHP var_dump() 会返回变量的数据类型和值:

实例

<span style="font-size: 14px;"><?php <br/>$x = 5985;<br/>var_dump($x);<br/>echo "<br>"; <br/>$x = -345; // 负数var_dump($x);<br/>echo "<br>"; <br/>$x = 0x8C; // 十六进制数var_dump($x);<br/>echo "<br>";<br/>$x = 047; // 八进制数var_dump($x);<br/>?><br/></span>

运行结果:

int(5985)
int(-345)
int(140)
int(39)

5.3 PHP 浮点数

浮点数是有小数点或指数形式的数字。

PHP var_dump() 会返回变量的数据类型和值:

实例

<span style="font-size: 14px;"><?php <br/>$x = 10.365;<br/>var_dump($x);<br/>echo "<br>"; <br/>$x = 2.4e3;<br/>var_dump($x);<br/>echo "<br>"; <br/>$x = 8E-5;<br/>var_dump($x);<br/>?><br/></span>

运行结果:

float(10.365)
float(2400)
float(8.0E-5)

5.4 PHP 逻辑

逻辑是 true 或 false。

<span style="font-size: 14px;">$x=true;<br/>$y=false;<br/></span>

逻辑常用于条件测试。  

5.5 PHP 数组

数组在一个变量中存储多个值。

在下面的例子中,我们将测试不同的数组。PHP var_dump() 会返回变量的数据类型和值:

实例

<span style="font-size: 14px;"><?php <br/>$cars=array("Volvo","BMW","SAAB");<br>var_dump($cars);<br>?><br></span>

运行结果:

array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(4) "SAAB" }

 相关文章推荐:《php常用数组函数有哪些?PHP常用数组函数总结整理

5.6 PHP 对象

对象是存储数据和有关如何处理数据的信息的数据类型。

在 PHP 中,必须明确地声明对象。

首先我们必须声明对象的类。对此,我们使用 class 关键词。类是包含属性和方法的结构。

然后我们在对象类中定义数据类型,然后在该类的实例中使用此数据类型:

实例

<span style="font-size: 14px;"><?php<br/>class Car<br/>{<br/>  var $color;<br/>  function Car($color="green") {<br/>    $this->color = $color;<br/>  }<br/>  function what_color() {<br/>    return $this->color;<br/>  }<br/>}<br/>?><br/></span>

 

5.7 PHP NULL 值

特殊的 NULL 值表示变量无值。NULL 是数据类型 NULL 唯一可能的值。

NULL 值标示变量是否为空。也用于区分空字符串与空值数据库。

可以通过把值设置为 NULL,将变量清空:

实例

<span style="font-size: 14px;"><?php<br/>$x="Hello world!";<br/>$x=null;<br/>var_dump($x);<br/>?></span>

6、PHP 字符串函数

字符串是字符序列,比如 "Hello world!"。

6.1 PHP strlen() 函数

strlen() 函数返回字符串的长度,以字符计。

下例返回字符串 "Hello world!" 的长度:

实例

<span style="font-size: 14px;"><?php<br/>echo strlen("Hello world!");<br/>?></span>

以上代码的输出是:12

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

6.2 PHP strpos() 函数

strpos() 函数用于检索字符串内指定的字符或文本。

如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。

下例检索字符串 "Hello world!" 中的文本 "world":

实例

<span style="font-size: 14px;"><?php<br/>echo strpos("Hello world!","world");<br/>?><br/></span>

以上代码的输出是:6。

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

7、PHP 常量

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

7.1 PHP 常量

常量是单个值的标识符(名称)。在脚本中无法改变该值。

有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。

注释:与变量不同,常量贯穿整个脚本是自动全局的。

7.2 设置 PHP 常量

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

  1. 首个参数定义常量的名称

  2. 第二个参数定义常量的值

  3. 可选的第三个参数规定常量名是否对大小写敏感。默认是 false。

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

实例

<span style="font-size: 14px;"> <!DOCTYPE html><br/><html><br/><body><br/><br/><?php<br/>// 定义对大小写敏感的常量<br/>define("GREETING", "Welcome to W3School.com.cn!");<br/>echo GREETING;<br/>echo "<br>";<br/>// 不会输出常量的值<br/>echo greeting;<br/>?>  <br/><br/></body><br/></html><br/></span>

 

运行结果:

 Welcome to W3School.com.cn!
greeting

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

实例

<span style="font-size: 14px;"> <!DOCTYPE html><br/><html><br/><body><br/><br/><?php<br/>// 定义对大小写不敏感的常量<br/>define("GREETING", "Welcome to W3School.com.cn!", true);<br/>echo GREETING;<br/>echo "<br>";<br/>// 会输出常量的值<br/>echo greeting;<br/>?>  <br/><br/></body><br/></html><br/></span>

 

运行结果:

Welcome to W3School.com.cn!
Welcome to W3School.com.cn!

8、PHP 运算符

下面介绍可用于 PHP 脚本中的各种运算符.

8.1 PHP 算数运算符

php算数运算符

运算符

名称

例子

结果

+

加法

$x + $y

$x与$y求和

减法

$x - $y

$x与$y待的差数

*

乘法

$x * $y

$x与$y的乘积

/

除法

$x / $y

$x与$y的商数

%

模数

$x % $y

$x与$y的余数

下例展示了使用不同算数运算符的不同结果:

实例

<span style="font-size: 14px;"><?php <br/>$x=10; <br>$y=6;<br>echo ($x + $y); // 输出 16echo ($x - $y); // 输出 4echo ($x * $y); // 输出 60echo ($x / $y); <br>// 输出 1.6666666666667echo ($x % $y); // 输出 4?><br></span>

 

8.2 PHP 赋值运算符

PHP 赋值运算符用于向变量写值。

PHP 中基础的赋值运算符是 "="。这意味着右侧赋值表达式会为左侧运算数设置值。

 

PHP 赋值运算符

赋值

等同于

描述

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

模数

 

 

 

 

 

 

 

 

 

 

 

 

下例展示了使用不同赋值运算符的不同结果:

实例

<span style="font-size: 14px;"><?php <br/>$x=10; <br>echo $x; // 输出 10$y=20; <br>$y += 100;<br>echo $y; // 输出 120$z=50;<br>$z -= 25;<br>echo $z; // 输出 25$i=5;<br>$i *= 6;<br>echo $i; // 输出 30$j=10;<br>$j /= 5;<br>echo $j; // 输出 2$k=15;<br>$k %= 4;<br>echo $k; // 输出 3?><br></span>

 

8.3 PHP 字符串运算符

PHP 字符串运算符

运算符

名称

例子

结果

.

串接

$txt1 = "Hello" $txt2 = $txt1 . " world!"

现在 $txt2 包含 "Hello world!"

.=

串接赋值

$txt1 = "Hello" $txt1 .= " world!"

现在 $txt1 包含 "Hello world!"

 

下例展示了使用字符串运算符的结果:

实例

<span style="font-size: 14px;"><?php <br/>$a = "Hello";<br>$b = $a . " world!";<br>echo $b; // 输出 Hello world!$x="Hello";<br>$x .= " world!";<br>echo $x; // 输出 Hello world!?><br></span>

8.4 PHP 递增/递减运算符

PHP 递增/递减运算符

运算符

名称

描述

++$x

前递增

$x 加一递增,然后返回 $x

$x++

后递增

返回 $x,然后 $x 加一递增

--$x

前递减

$x 减一递减,然后返回 $x

$x--

后递减

返回 $x,然后 $x 减一递减 

下例展示了使用不同递增/递减运算符的不同结果:

实例

<span style="font-size: 14px;"><?php <br/>$x=10; <br>echo ++$x; // 输出 11$y=10; <br>echo $y++; // 输出 10$z=5;<br>echo --$z; // 输出 4$i=5;<br>echo $i--; // 输出 5?><br></span>

8.5 PHP 比较运算符

PHP 比较运算符用于比较两个值(数字或字符串):

PHP 比较运算符

运算符

名称

例子

结果

==

等于

$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

 

下例展示了使用某些比较运算符的不同结果:

实例

<span style="font-size: 14px;"><br><br><br><br><?php <br/>$x=100; <br>$y="100";<br><br>var_dump($x == $y); // 因为值相等,返回 true<br>echo "<br>";<br>var_dump($x === $y); // 因为类型不相等,返回 false<br>echo "<br>";<br>var_dump($x != $y); // 因为值相等,返回 false<br>echo "<br>";<br>var_dump($x !== $y); // 因为值不相等,返回 true<br>echo "<br>";<br><br>$a=50;<br>$b=90;<br><br>var_dump($a > $b);<br>echo "<br>";<br>var_dump($a ?>   <br><br><br><br></span>

 

8.6 PHP 逻辑运算符

PHP 逻辑运算符

运算符

名称

例子

结果

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。

 

8.7 PHP 数组运算符

PHP 数组运算符用于比较数组:

PHP 数组运算符

运算符

名称

例子

结果

+

联合

$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。

 

下例展示了使用不同数组运算符的不同结果:

实例

<span style="font-size: 14px;"><br><br><br><br><?php <br/>$x = array("a" => "red", "b" => "green"); <br>$y = array("c" => "blue", "d" => "yellow"); <br>$z = $x + $y; // $x 与 $y 的联合<br>var_dump($z);<br>echo "<br>";<br>var_dump($x == $y);<br>echo "<br>";<br>var_dump($x === $y);<br>echo "<br>";<br>var_dump($x != $y);<br>echo "<br>";<br>var_dump($x  $y);<br>echo "<br>";<br>var_dump($x !== $y);<br>?>   <br><br><br><br></span>


运行结果:

array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" }
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)

 

9、PHP if...else...elseif 语句

条件语句用于基于不同条件执行不同的动作

9.1 PHP 条件语句

在您编写代码时,经常会希望为不同的决定执行不同的动作。您可以在代码中使用条件语句来实现这一点。

在 PHP 中,我们可以使用以下条件语句:

  • if 语句 - 如果指定条件为真,则执行代码

  • if...else 语句 - 如果条件为 true,则执行代码;如果条件为 false,则执行另一端代码

  • if...elseif....else 语句 - 选择若干段代码块之一来执行

  • switch 语句 - 语句多个代码块之一来执行

9.2 PHP - if 语句

if 语句用于在指定条件为 true 时执行代码。

语法

<span style="font-size: 14px;">if (条件) {<br>  当条件为 true 时执行的代码;<br>}<br></span>

下例将输出 "Have a good day!",如果当前时间 (HOUR) 小于 20:

实例

<span style="font-size: 14px;"><?php <br/>$t=date("H");<br><br>if ($t  echo "Have a good day!";<br>}<br>?><br></span>

9.3 PHP - if...else 语句

请使用 if....else 语句在条件为 true 时执行代码,在条件为 false 时执行另一段代码。

语法

<span style="font-size: 14px;">if (条件) {<br>  条件为 true 时执行的代码;<br>} else {<br>  条件为 false 时执行的代码;<br>}<br></span>

下例将输出 "Have a good day!",如果当前时间 (HOUR) 小于 20,否则输出 "Have a good night!":

实例

<span style="font-size: 14px;"><?php <br/>$t=date("H");<br><br>if ($t  echo "Have a good day!";<br>} else {<br>  echo "Have a good night!";<br>}<br>?><br></span>

9.4 PHP - if...elseif....else 语句

请使用 if....elseif...else 语句来选择若干代码块之一来执行。

语法

<span style="font-size: 14px;">if (条件) {<br>  条件为 true 时执行的代码;<br>} elseif (condition) {<br>  条件为 true 时执行的代码;<br>} else {<br>  条件为 false 时执行的代码;<br>}<br></span>

下例将输出 "Have a good morning!",如果当前时间 (HOUR) 小于 10,如果当前时间小于 20,则输出 "Have a good day!"。否则将输出 "Have a good night!":

实例

<span style="font-size: 14px;"><?php <br/>$t=date("H");<br><br>if ($t  echo "Have a good morning!";<br>} elseif ($t  echo "Have a good day!";<br>} else {<br>  echo "Have a good night!";<br>}<br>?><br></span>

 

9.5 PHP Switch 语句

switch 语句用于基于不同条件执行不同动作。

如果希望有选择地执行若干代码块之一,请使用 Switch 语句。

使用 Switch 语句可以避免冗长的 if..elseif..else 代码块。

语法

<span style="font-size: 14px;">switch (expression)<br>{<br>case label1:<br>  code to be executed if expression = label1;<br>  break;  <br>case label2:<br>  code to be executed if expression = label2;<br>  break;<br>default:<br>  code to be executed<br>  if expression is different <br>  from both label1 and label2;<br>}<br></span>

工作原理:

  1. 对表达式(通常是变量)进行一次计算

  2. 把表达式的值与结构中 case 的值进行比较

  3. 如果存在匹配,则执行与 case 关联的代码

  4. 代码执行后,break 语句阻止代码跳入下一个 case 中继续执行

  5. 如果没有 case 为真,则使用 default 语句

实例

<span style="font-size: 14px;"><?php <br/>switch ($x)<br>{<br>case 1:<br>  echo "Number 1";<br>  break;<br>case 2:<br>  echo "Number 2";<br>  break;<br>case 3:<br>  echo "Number 3";<br>  break;<br>default:<br>  echo "No number between 1 and 3";<br>}<br>?><br><br></span>

The above is the detailed content of Basic introductory knowledge of Php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!