Toán tử PHP
Toán tử PHP
Toán tử được sử dụng để thực hiện các thao tác trên các biến và giá trị.
PHP chia các toán tử thành các nhóm sau:
- Toán tử số học
- Toán tử gán
- Toán tử so sánh
- Toán tử tăng/giảm
- Toán tử logic
- Toán tử chuỗi
- Toán tử mảng
- Toán tử gán có điều kiện
Toán tử số học PHP
Các toán tử số học PHP được sử dụng với các giá trị số để thực hiện các phép toán số học phổ biến, chẳng hạn như cộng, trừ, nhân, v.v.
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y | Try it » |
- | Subtraction | $x - $y | Difference of $x and $y | Try it » |
* | Multiplication | $x * $y | Product of $x and $y | Try it » |
/ | Division | $x / $y | Quotient of $x and $y | Try it » |
% | Modulus | $x % $y | Remainder of $x divided by $y | Try it » |
** | Exponentiation | $x ** $y | Result of raising $x to the $y'th power | Try it » |
Toán tử gán PHP
Toán tử gán PHP được sử dụng với các giá trị số để ghi giá trị vào một biến.
Toán tử gán cơ bản trong PHP là "=". Điều đó có nghĩa là toán hạng bên trái được đặt thành giá trị của biểu thức gán ở bên phải.
Assignment | Same as... | Description | Try it |
---|---|---|---|
x = y | x = y | The left operand gets set to the value of the expression on the right | Try it » |
x += y | x = x + y | Addition | Try it » |
x -= y | x = x - y | Subtraction | Try it » |
x *= y | x = x * y | Multiplication | Try it » |
x /= y | x = x / y | Division | Try it » |
x %= y | x = x % y | Modulus | Try it » |
Toán tử so sánh PHP
Toán tử so sánh PHP được sử dụng để so sánh hai giá trị (số hoặc chuỗi):
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
== | Equal | $x == $y | Returns true if $x is equal to $y | Try it » |
=== | Identical | $x === $y | Returns true if $x is equal to $y, and they are of the same type | Try it » |
!= | Not equal | $x != $y | Returns true if $x is not equal to $y | Try it » |
<> | Not equal | $x <> $y | Returns true if $x is not equal to $y | Try it » |
!== | Not identical | $x !== $y | Returns true if $x is not equal to $y, or they are not of the same type | Try it » |
> | Greater than | $x > $y | Returns true if $x is greater than $y | Try it » |
< | Less than | $x < $y | Returns true if $x is less than $y | Try it » |
>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y | Try it » |
<= | Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y | Try it » |
<=> | Spaceship | $x <=> $y | Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. | Try it » |
Toán tử tăng/giảm trong PHP
Toán tử tăng trong PHP được sử dụng để tăng giá trị của biến.
Các toán tử giảm dần trong PHP được sử dụng để giảm giá trị của một biến.
Operator | Same as... | Description | Try it |
---|---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x | Try it » |
$x++ | Post-increment | Returns $x, then increments $x by one | Try it » |
--$x | Pre-decrement | Decrements $x by one, then returns $x | Try it » |
$x-- | Post-decrement | Returns $x, then decrements $x by one | Try it » |
Toán tử logic PHP
Các toán tử logic PHP được sử dụng để kết hợp các câu lệnh có điều kiện.
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
and | And | $x and $y | True if both $x and $y are true | Try it » |
or | Or | $x or $y | True if either $x or $y is true | Try it » |
xor | Xor | $x xor $y | True if either $x or $y is true, but not both | Try it » |
&& | And | $x && $y | True if both $x and $y are true | Try it » |
|| | Or | $x || $y | True if either $x or $y is true | Try it » |
! | Not | !$x | True if $x is not true | Try it » |
Toán tử chuỗi PHP
PHP có hai toán tử được thiết kế đặc biệt cho chuỗi.
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Concatenation of $txt1 and $txt2 | Try it » |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends $txt2 to $txt1 | Try it » |
Toán tử mảng PHP
Các toán tử mảng PHP được sử dụng để so sánh các mảng.
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
+ | Union | $x + $y | Union of $x and $y | Try it » |
== | Equality | $x == $y | Returns true if $x and $y have the same key/value pairs | Try it » |
=== | Identity | $x === $y | Returns true if $x and $y have the same key/value pairs in the same order and of the same types | Try it » |
!= | Inequality | $x != $y | Returns true if $x is not equal to $y | Try it » |
<> | Inequality | $x <> $y | Returns true if $x is not equal to $y | Try it » |
!== | Non-identity | $x !== $y | Returns true if $x is not identical to $y | Try it » |
Toán tử gán có điều kiện trong PHP
Toán tử gán điều kiện trong PHP được sử dụng để đặt giá trị tùy theo điều kiện:
Operator | Name | Example | Result | Try it |
---|---|---|---|---|
?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE |
Try it » |
?? | Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2 . Introduced in PHP 7 |
Try it » |