运算符
操作符用于对某些值执行操作。换句话说,我们可以将运算符描述为接受一些值,对它们执行一些操作并给出结果的东西。从示例中可以看出,表达式‘+’中的“1 + 2 = 3”是一个运算符。它取两个值1和2,对它们执行加法运算得到3。与任何其他编程语言一样,PHP也支持各种类型的操作,比如算术操作(加法、减法等)、逻辑操作(和,或等)、递增/递减操作等。因此,PHP为我们提供了许多操作符来对各种操作数、变量或值执行此类操作。这些操作符只是执行各种类型操作所需的符号。以下是不同组别的操作员:
- 算术运算符
- 逻辑或关系运算符
- 比较运算符
- 条件运算符或三元运算符
- 赋值运算符
- 太空船操作员(PHP 7中介绍)
- 数组运算符
- 递增/递减运算符
- 字符串操作符
现在让我们详细了解这些操作符:
算术运算符
算术运算符用于执行简单的数学运算,如加法、减法、乘法等。下面是一系列算术运算符,以及PHP提供的语法和操作:
Operator | Name | Syntax | Operation |
---|---|---|---|
+ | 加号 | $x + $y | 和操作数 |
– | 减号 | $x – $y | 差操作数 |
* | 乘号 | $x * $y | 乘操作数 |
/ | 除号 | $x / $y | 除操作数 |
** | 取幂 | $x ** $y | 取幂操作数 |
% | 取模 | $x % $y | 取模操作数 |
注意:PHP 5.6中已经介绍了求幂。
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php // variable 1 $x = 29; // variable 2 $y = 4; // some arithmetic operations on // these two variables echo ($x + $y), "\n"; echo($x - $y), "\n"; echo($x * $y), "\n"; echo($x / $y), "\n"; echo($x % $y), "\n"; ?> |
输出
1 2 3 4 5 | 33 25 116 7.25 1 |
逻辑或关系运算符
这些基本用于操作条件语句和表达式。条件语句是基于条件的。同样,条件可以满足也可以不满足,因此条件语句的结果可以是真,也可以是假。下面是PHP提供的逻辑运算符以及语法和操作:
Operator | Name | Syntax | Operation |
---|---|---|---|
and | Logical AND | $x and $y | 如果两个操作数都为真,则为真 |
or | Logical OR | $x or $y | 如果操作数之一为真,则为真,否则为假 |
xor | Logical XOR | $x xor $y | 如果操作数之一为真,则为真;如果两个操作数都为真,则为假 |
&& | Logical AND | $x && $y | 如果两个操作数都为真,则为真 |
|| | Logical OR | $x || $y | 如果操作数之一为真,则为真,否则为假 |
! | Logical NOT | !$x | 如果$x为假,则为真 |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $x = 50; $y = 30; if ($x == 50 and $y == 30) echo "and Success \n"; if ($x == 50 or $y == 20) echo "or Success \n"; if ($x == 50 xor $y == 20) echo "xor Success \n"; if ($x == 50 && $y == 30) echo "&& Success \n"; if ($x == 50 || $y == 20) echo "|| Success \n"; if (!$z) echo "! Success \n"; ?> |
输出
1 2 3 4 5 6 | and Success or Success xor Success && Success || Success ! Success |
比较运算符
这些操作符用于比较两个元素并以布尔形式输出结果。下面是比较运算符以及PHP提供的语法和操作:
Operator | Name | Syntax | Operation |
---|---|---|---|
== | Equal To | $x == $y | Returns True if both the operands are equal |
!= | Not Equal To | $x != $y | Returns True if both the operands are not equal |
<> | Not Equal To | $x != $y | Returns True if both the operands are unequal |
=== | Identical | $x === $y | Returns True if both the operands are equal and are of the same type |
!== | Not Identical | $x == $y | Returns True if both the operands are unequal and are of different types |
< | Less Than | $x < $y | Returns True if $x is less than $y |
> | Greater Than | $x > $y | Returns True if $x is greater than $y |
<= | Less Than or Equal To | $x <= $y | Returns True if $x is less than or equal to $y |
>= | Greater Than or Equal To | $x >= $y | Returns True if $x is greater than or equal to $y |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $a = 80; $b = 50; $c = "80"; // Here var_dump function has been used to // display structured information. We will learn // about this function in complete details in further // articles. var_dump($a == $c) + "\n"; var_dump($a != $b) + "\n"; var_dump($a <> $b) + "\n"; var_dump($a === $c) + "\n"; var_dump($a !== $c) + "\n"; var_dump($a < $b) + "\n"; var_dump($a > $b) + "\n"; var_dump($a <= $b) + "\n"; var_dump($a >= $b); ?> |
输出
1 2 3 4 5 6 7 8 9 | bool(true) bool(true) bool(true) bool(false) bool(true) bool(false) bool(true) bool(false) bool(true) |
条件运算符或三元运算符
这些运算符用于比较两个值,并根据结果是真还是假,同时获取两个结果。这些也是我们将在关于决策的文章中读到的if…else语句的简写符号。
语法
1 | $var = (condition)? value1 : value2; |
在这里,条件值要么为真,要么为假。如果条件求值为True,那么value1将被分配给变量$var,否则value2将被分配给它。
Operator | Name | Operation |
---|---|---|
?: | Ternary | If condition is true ? then $x : or else $y. This means that if condition is true then left result of the colon is accepted otherwise the result on right. |
例子
1 2 3 4 5 6 7 | <?php $x = -12; echo ($x > 0) ? 'The number is positive' : 'The number is negative'; ?> |
输出
1 | The number is negative |
赋值运算符
这些操作符用于为不同的变量赋值,不管是否有中间操作。下面是赋值运算符以及PHP提供的语法和操作:
Operator | Name | Syntax | Operation |
---|---|---|---|
= | Assign | $x = $y | Operand on the left obtains the value of the operand on right |
+= | Add then Assign | $x += $y | Simple Addition same as $x = $x + $y |
-= | Subtract then Assign | $x -= $y | Simple subtraction same as $x = $x – $y |
*= | Multiply then Assign | $x *= $y | Simple product same as $x = $x * $y |
/= | Divide then Assign (quotient) | $x /= $y | Simple division same as $x = $x / $y |
%= | Divide then Assign (remainder) | $x %= $y | Simple division same as $x = $x % $y |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php // simple assign operator $y = 75; echo $y, "\n"; // add then assign operator $y = 100; $y += 200; echo $y, "\n"; // subtract then assign operator $y = 70; $y -= 10; echo $y, "\n"; // multiply then assign operator $y = 30; $y *= 20; echo $y, "\n"; // Divide then assign(quotient) operator $y = 100; $y /= 5; echo $y, "\n"; // Divide then assign(remainder) operator $y = 50; $y %= 5; echo $y; ?> |
输出
1 2 3 4 5 6 | 75 300 60 600 20 0 |
数组运算符
这些运算符用于数组。这里是数组操作符以及PHP提供的语法和操作:
Operator | Name | Syntax | Operation |
---|---|---|---|
+ | Union | $x + $y | Union of both i.e., $x and $y |
== | Equality | $x == $y | Returns true if both has same key-value pair |
!= | Inequality | $x != $y | Returs True if both are unequal |
=== | Identity | $x === $y | Returns True if both has same key-value pair in the same order and of same type |
!== | Non-Identity | $x !== $y | Returns True if both are not identical to each other |
<> | Inequality | $x <> $y | Returns True if both are unequal |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $x = array("k" => "Car", "l" => "Bike"); $y = array("a" => "Train", "b" => "Plane"); var_dump($x + $y); var_dump($x == $y) + "\n"; var_dump($x != $y) + "\n"; var_dump($x <> $y) + "\n"; var_dump($x === $y) + "\n"; var_dump($x !== $y) + "\n"; ?> |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | array(4) { ["k"]=> string(3) "Car" ["l"]=> string(4) "Bike" ["a"]=> string(5) "Train" ["b"]=> string(5) "Plane" } bool(false) bool(true) bool(true) bool(false) bool(true) |
递增/递减运算符
这些操作符称为一元操作符,因为它在单个操作数上工作。它们用于递增或递减值。
Operator | Name | Syntax | Operation |
---|---|---|---|
++ | Pre-Increment | $x++ | First returns $x, then increment it by one |
— | Pre-Decrement | $x– | First returns $x, then decrement it by one |
++ | Post-Increment | ++$x | First increments $x by one, then return $x |
++ | Post-Increment | ++$x | First increments $x by one, then return $x |
— | Post-Decrement | –$x | First decrements $x by one, then return $x |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $x = 2; echo ++$x, " First increments then prints \n"; echo $x, "\n"; $x = 2; echo $x++, " First prints then increments \n"; echo $x, "\n"; $x = 2; echo --$x, " First decrements then prints \n"; echo $x, "\n"; $x = 2; echo $x--, " First prints then decrements \n"; echo $x; ?> |
输出
1 2 3 4 5 6 7 8 | 3 First increments then prints 3 2 First prints then increments 3 1 First decrements then prints 1 2 First prints then decrements 1 |
字符串操作符
这些是通过字符串实现的。
Operator | Name | Syntax | Operation |
---|---|---|---|
. | Concatenation | $x.$y | Concatenated $x and $y |
.= | Concatenation and assignment | $x.=$y | First concatenates then assigns, same as $x = $x.$y |
例子
1 2 3 4 5 6 7 8 9 10 11 | <?php $x = "sky8g"; $y = "for"; $z = "sky8g!!!"; echo $x . $y . $z, "\n"; $x .= $y . $z; echo $x; ?> |
输出
1 2 | sky8gforsky8g!!! sky8gforsky8g!!! |
太空船操作员(PHP 7中介绍)
PHP 7引入了一种新的操作符,称为飞船操作符()。这些操作符用于比较值,但它不返回布尔结果,而是返回整数值。如果两个操作数都相等,则返回0。如果正确的操作数更大,则返回-1。如果左操作数更大,则返回1。下表详细展示了它的工作原理:
Operator | Syntax | Operation |
---|---|---|
$x < $y | $x <=> $y | Identical to -1 (right is greater) |
$x > $y | $x <=> $y | Identical to 1 (left is greater) |
$x <= $y | $x <=> $y | Identical to -1 (right is greater) or identical to 0 (if both are equal) |
$x >= $y | $x <=> $y | Identical to 1 (if left is greater) or identical to 0 (if both are equal) |
$x == $y | $x <=> $y | Identical to 0 (both are equal) |
$x != $y | $x <=> $y | Not Identical to 0 |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php $x = 50; $y = 50; $z = 25; echo $x <=> $y; echo "\n"; echo $x <=> $z; echo "\n"; echo $z <=> $y; echo "\n"; // We can do the same for Strings $x = "Ram"; $y = "Krishna"; echo $x <=> $y; echo "\n"; echo $x <=> $y; echo "\n"; echo $y <=> $x; ?> |
输出
1 2 3 4 5 6 | 0 1 -1 1 1 -1 |