什么是 PHP 中的运算符
运算符是告诉 PHP 处理器执行某些操作的符号。例如, 加法符号是告诉 PHP 添加两个变量或值的运算符,而大于 符号是告诉 PHP 比较两个值的运算符。+
`>`
以下列表描述了 PHP 中使用的不同运算符。
PHP 算术运算符
算术运算符用于执行常见的算术运算,例如加法、减法、乘法等。以下是 PHP 的算术运算符的完整列表:
算子 | 描述 | 例 | 结果 |
---|---|---|---|
+ | 加法 | $x + $y | $x 和 $y 之和 |
- | 减法 | $x - $y | $x 和 $y 的差异。 |
* | 乘法 | $x * $y | $x 和 $y 的产物。 |
/ | 划分 | $x / $y | $x 和 $y 的商 |
% | 模 | $x % $y | $x 除以 $y的余数 |
以下示例将向您展示这些算术运算符的实际应用:
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14
echo($x - $y); // 0utputs: 6
echo($x * $y); // 0utputs: 40
echo($x / $y); // 0utputs: 2.5
echo($x % $y); // 0utputs: 2
?>
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 |
以下示例将向您展示这些赋值运算符的实际应用:
<?php
$x = 10;
echo $x; // Outputs: 10
$x = 20;
$x += 30;
echo $x; // Outputs: 50
$x = 50;
$x -= 20;
echo $x; // Outputs: 30
$x = 5;
$x *= 25;
echo $x; // Outputs: 125
$x = 50;
$x /= 10;
echo $x; // Outputs: 5
$x = 100;
$x %= 15;
echo $x; // Outputs: 10
?>
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 > $y | 如果$x 大于 $y,则为 True |
>= | 大于或等于 | $x >= $y | 如果$x 大于或等于 $y,则为 True |
<= | 小于或等于 | $x <= $y | 如果$x 小于或等于 $y,则为 True |
以下示例将向您展示这些比较运算符的实际应用:
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z); // Outputs: boolean true
var_dump($x === $z); // Outputs: boolean false
var_dump($x != $y); // Outputs: boolean true
var_dump($x !== $z); // Outputs: boolean true
var_dump($x < $y); // Outputs: boolean true
var_dump($x > $y); // Outputs: boolean false
var_dump($x <= $y); // Outputs: boolean true
var_dump($x >= $y); // Outputs: boolean false
?>
PHP 递增和递减运算符
递增/递减运算符用于递增/递减变量的值。
算子 | 名字 | 影响 |
---|---|---|
++$x | 预增量 | $x 递增 1,然后返回 $x |
$x++ | 后增量 | 返回$x,然后将 $x 递增 1 |
—$x | 递减前 | $x递减1,然后返回$x |
$x— | 递减后 | 返回$x,然后将 $x 递减 1 |
以下示例将向您展示这些递增和递减运算符的实际应用:
<?php
$x = 10;
echo ++$x; // Outputs: 11
echo $x; // Outputs: 11
$x = 10;
echo $x++; // Outputs: 10
echo $x; // Outputs: 11
$x = 10;
echo --$x; // Outputs: 9
echo $x; // Outputs: 9
$x = 10;
echo $x--; // Outputs: 10
echo $x; // Outputs: 9
?>
PHP 逻辑运算符
The logical operators are typically used to combine conditional statements.
Operator | Name | Example | Result | ||||
---|---|---|---|---|---|---|---|
and | And | $x and $y | True if both$x and $y are true | ||||
or | Or | $x or $y | True if either$x or $y is true | ||||
xor | Xor | $x xor $y | True if either$x or $y is true, but not both | ||||
&& | And | $x && $y | True if both$x and $y are true | ||||
\ | \ | Or | $x\ | \ | $y | True if either $x or $y is true | |
! | Not | !$x | True if$x is not true |
The following example will show you these logical operators in action:
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
?>
PHP 字符串运算符
有两个专门为字符串设计的运算符。
算子 | 描述 | 例 | 结果 |
---|---|---|---|
. | 串联 | $str 1 .$str 2 | $str 1 和 $str 2 的串联 |
.= | 串联赋值 | $str 1 .= $str 2 | 将$str 2 附加到 $str 1 |
以下示例将向您展示这些字符串运算符的实际应用:
<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Outputs: Hello World!
$x .= $y;
echo $x; // Outputs: Hello World!
?>
PHP 数组运算符
The array operators are used to compare arrays:
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $x + $y | Union of$x and $y |
== | Equality | $x == $y | True if$x and $y have the same key/value pairs |
=== | Identity | $x === $y | True if$x and $y have the same key/value pairs in the same order and of the same types |
!= | Inequality | $x != $y | True if$x is not equal to $y |
<> | Inequality | $x <> $y | True if$x is not equal to $y |
!== | Non-identity | $x !== $y | True if$x is not identical to $y |
The following example will show you these array operators in action:
<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y); // Outputs: boolean false
var_dump($x === $y); // Outputs: boolean false
var_dump($x != $y); // Outputs: boolean true
var_dump($x <> $y); // Outputs: boolean true
var_dump($x !== $y); // Outputs: boolean true
?>
PHP Spaceship Operator PHP 7
PHP 7 introduces a new spaceship operator (<=>) which can be used for comparing two expressions. It is also known as combined comparison operator.
The spaceship operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It basically provides three-way comparison as shown in the following table:
Operator | <=> Equivalent |
---|---|
$x < $y | ($x <=> $y) === -1 |
$x <= $y | ($x <=> $y) === -1 |
$x == $y | ($x <=> $y) === 0 |
$x != $y | ($x <=> $y) !== 0 |
$x >= $y | ($x <=> $y) === 1 |
$x > $y | ($x <=> $y) === 1 |
The following example will show you how spaceship operator actually works:
<?php
// Comparing Integers
echo 1 <=> 1; // Outputs: 0
echo 1 <=> 2; // Outputs: -1
echo 2 <=> 1; // Outputs: 1
// Comparing Floats
echo 1.5 <=> 1.5; // Outputs: 0
echo 1.5 <=> 2.5; // Outputs: -1
echo 2.5 <=> 1.5; // Outputs: 1
// Comparing Strings
echo "x" <=> "x"; // Outputs: 0
echo "x" <=> "y"; // Outputs: -1
echo "y" <=> "x"; // Outputs: 1
?>
此处评论已关闭