PHP Operators
In this chapter, we will discuss the application of different operators in PHP.
In PHP, the assignment operator = is used to assign a value to a variable.
In PHP, the arithmetic operator + is used to add values together.
PHP Arithmetic Operators
| operator | Name | Description | Example | Result |
|---|---|---|---|---|
| x + y | Addition | Sum of x and y | 2 + 2 | 4 |
| x - y | Subtraction | Difference of x and y | 5 - 2 | 3 |
| x * y | Multiplication | Product of x and y | 5 * 2 | 10 |
| x / y | Division | Quotient of x and y | 15 / 5 | 3 |
| x % y | Modulus (remainder of division) | Remainder of x divided by y | 5 % 2 |
10 % 8
10 % 2 | 1
2
0 |
| -x | Set negative | Take the opposite sign of x |
| -2 | | ~x | Bitwise NOT |x is negated by performing a bitwise "NOT" operation. The rule is:
~1=-2;| | -3 | | a . b | Concatenation | Join two strings | "Hi" . "Ha" | HiHa |
~0=-1;
The following example demonstrates the different results obtained using different arithmetic operators:
Example
' ; // newline echo ( $x - $y ) ; // output 4 echo '' ; // newline echo ( $x * $y ) ; // output 60 echo '
' ; // newline echo ( $x / $y ) ; // output 1.6666666666667 echo '
' ; // newline echo ( $x % $y ) ; // output 4 echo '
' ; // newline echo - $x ; ?>
Try it »
PHP 7+ Added Integer Division Operator intdiv(), which returns the value of the first parameter divided by the second, rounded down to an integer. Example:
Example
The above example will output:
int(3)
PHP Assignment Operators
In PHP, the basic assignment operator is =. It means the left operand is set to the value of the right-side expression. That is, the value of $x = 5 is 5.
| operator | is equivalent to | Description |
|---|---|---|
| x = y | x = y | The left operand is set to the value of the right-side expression |
| x += y | x = x + y | Addition |
| x -= y | x = x - y | Subtraction |
| x *= y | x = x * y | Multiplication |
| x /= y | x = x / y | Division |
| x %= y | x = x % y | Modulus (remainder of division) |
| a .= b | a = a . b | Concatenate two strings |
The following example demonstrates the different results obtained using different assignment operators:
Example
Try it »
The following example demonstrates the same result obtained using different string operators:
Example
Try it »
PHP Increment/Decrement Operators
| operator | Name | Description |
|---|---|---|
| ++ x | Pre-increment | Increment x by 1, then return x |
| x ++ | Post-increment | Return x, then increment x by 1 |
| -- x | Pre-decrement | Decrement x by 1, then return x |
| x -- | Post-decrement | Return x, then decrement x by 1 |
The following example demonstrates the results obtained using increment/decrement operators:
Example
Try it »
PHP Comparison Operators
Comparison operators let you compare two values:
| operator | Name | Description | Example |
|---|---|---|---|
| x == y | Equal | Returns true if x is equal to y | 5==8 returns false |
| x === y | Identical | Returns true if x is equal to y and they are of the same type | 5==="5" returns false |
| x != y | Not equal | Returns true if x is not equal to y | 5!=8 returns true |
| x <> y | Not equal | Returns true if x is not equal to y | 5<>8 returns true |
| x !== y | Not identical | Returns true if x is not equal to y, or they are of different types | 5!=="5" returns true |
| x > y | Greater than | Returns true if x is greater than y | 5>8 returns false |
| x < y | Less than | Returns true if x is less than y | 5<8 returns true |
| x >= y | Greater than or equal to | Returns true if x is greater than or equal to y | 5>=8 returns false |
| x <= y | Less than or equal to | Returns true if x is less than or equal to y | 5<=8 returns true |
The following example demonstrates the different results obtained using some comparison operators:
Example
" ; var_dump ( $x === $y ) ; echo "" ; var_dump ( $x != $y ) ; echo "
" ; var_dump ( $x !== $y ) ; echo "
" ; $a = 50 ; $b = 90 ; var_dump ( $a > $b ) ; echo "
" ; var_dump ( $a < $b ) ; ?>
Try it »
PHP Logical Operators
| operator | Name | Description | Example |
|---|---|---|---|
| x and y | The difference between | Returns true if both x and y are true | x=6 |
y=3
(x < 10 and y > 1) 返回 true |
| x or y | Or | Returns true if at least one of x and y is true | x=6
y=3
(x==6 or y==5) returns true | | x xor y | Xor | Returns true if exactly one of x and y is true | x=6
y=3
(x==6 xor y==3) returns false | | x && y | And | Returns true if both x and y are true | x=6
y=3
(x < 10 && y > 1) returns true | | x || y | Or | Returns true if at least one of x and y is true | x=6
y=3
(x==5 || y==5) returns false | | ! x | Not | Returns true if x is not true | x=6
y=3
!(x==y) returns true |
PHP Array Operators
| operator | Name | Description |
|---|---|---|
| x + y | Union | The union of x and y |
| x == y | Equality | Returns true if x and y have the same key/value pairs |
| x === y | Identity | Returns true if x and y have the same key/value pairs, in the same order and of the same type |
| x != y | Inequality | Returns true if x is not equal to y |
| x <> y | Inequality | Returns true if x is not equal to y |
| x !== y | Non-identity | Returns true if x is not equal to y |
The following example demonstrates the different results obtained using some array operators:
Example
" red " , " b " => " green " ) ; $y = array ( " c " => " blue " , " d " => " yellow " ) ; $z = $x + $y ; // $x and $y array merge var_dump ( $z ) ; var_dump ( $x == $y ) ; var_dump ( $x === $y ) ; var_dump ( $x != $y ) ; var_dump ( $x <> $y ) ; var_dump ( $x !== $y ) ; ?>Try it »
Ternary Operator
Another conditional operator is the "?:" (or ternary) operator.
Syntax format
(expr1) ? (expr2) : (expr3)
The value is expr2 when expr1 evaluates to TRUE, and expr3 when expr1 evaluates to FALSE.
Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 when expr1 evaluates to TRUE, otherwise returns expr3.
Example
In the example below, by checking whether the $_GET request contains a user value, if it does, return $_GET['user'], otherwise return nobody:
Example
**Note:** PHP_EOL is a newline character, compatible with more platforms.
PHP 7+ added a NULL coalescing operator ??, as shown in the following example:
Example
Combined Comparison Operator (PHP 7+)
PHP 7+ supports the combined comparison operator, also known as the spaceship operator, with the symbol <=>. The combined comparison operator can easily implement comparison of two variables, and is not limited to numeric data comparison.
The syntax format is as follows:
$c = $a <=> $b;
The analysis is as follows:
- If ** $a > $b**, then $c has a value of 1.
- If both the $a == $b, then $c has a value of 0.
- If both the $a < $b, then $c has a value of -1.
The example is as follows:
Example
1 ; // 0 echo 1 <=> 2 ; // -1 echo 2 <=> 1 ; // 1 // float echo 1 .5 <=> 1 .5 ; // 0 echo 1 .5 <=> 2 .5 ; // -1 echo 2 .5 <=> 1 .5 ; // 1 // string echo " a " <=> " a " ; // 0 echo " a " <=> " b " ; // -1 echo " b " <=> " a " ; // 1 ?>Operator Precedence
The table below lists operators by precedence from highest to lowest. Operators on the same row have the same precedence, in which case their associativity determines the evaluation order.
Description: Left = from left to right, Right = from right to left.
| Associativity | operator | Additional Information |
|---|---|---|
| None | clone new | clone and new |
| Left | [ | array() |
| Right | ++ -- ~ (int) (float) (string) (array) (object) (bool) @ | Type and increment/decrement |
| None | instanceof | Type |
| Right | ! | Logical operators |
| Left | * / % | Arithmetic operators |
| Left | + – . | Arithmetic and string operators |
| Left | << >> | Bitwise operators |
| None | == != === !== <> | Comparison operators |
| Left | & | Bitwise and reference operators |
| Left | ^ | Bitwise operators |
| Left | | | Bitwise operators |
| Left | && | Logical operators |
| Left | || | Logical operators |
| Left | ? : | Ternary Operator |
| Right | = += -= *= /= .= %= &= |= ^= <<= >>= => | Assignment operators |
| Left | and | Logical operators |
| Left | xor | Logical operators |
| Left | or | Logical operators |
| Left | , | Used in many places |
In operator precedence, or and ||, && and and are all logical operators with the same effect, but their precedence is different.
Example
= > and // precedence: || > = > or $a = 3 ; $b = false ; $c = $a or $b ; var_dump ( $c ) ; // here $c is an int value 3, not a boolean value true $d = $a || $b ; var_dump ( $d ) ; // here $d is a boolean value true ?>The output of the above example is:
int(3)
bool(true)
Use of Parentheses
We use paired parentheses to explicitly mark the order of operations, rather than relying on operator precedence and associativity, which usually improves code readability.
Example
The output of the above example is:
7
9