PHP Operators
In this chapter, we will discuss the application of different operators in PHP.
In PHP, the assignment operator=is used to assign values to variables.
In PHP, the arithmetic operators+are used to add values together.
PHP Arithmetic Operators
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
| x + y | add | Sum of x and y | 2 + 2 | 4 |
| x - y | subtract | Difference of x and y | 5 - 2 | 3 |
| x * y | multiply | Product of x and y | 5 * 2 | 10 |
| x / y | divide | 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 | <?php $x = 2; echo -$x; ?> |
-2 |
| ~x | Bitwise not | Invert x, performing a bitwise NOT operation on its binary bits. The rules are: ~1=-2; ~0=-1; |
<?php $x = 2; echo ~$x; ?> |
-3 |
| a . b | Concatenation | Concatenate two strings | "Hi" . "Ha" | HiHa |
The following examples demonstrate the different results obtained using different arithmetic operators:
Example
Try it yourself »
PHP7+ version adds an integer division operatorintdiv(), this function returns the value of the first parameter divided by the second parameter and rounds it down (floor). Example:
Example
The above example will output:
int(3)
PHP Assignment Operators
In PHP, the basic assignment operator is=. It means that the left operand is set to the value of the right-hand side expression. That is,$x = 5the value is 5.
| Operator | Equivalent to | Description |
|---|---|---|
| x = y | x = y | The left operand is set to the value of the right-hand side expression |
| x += y | x = x + y | add |
| x -= y | x = x - y | subtract |
| x *= y | x = x * y | multiply |
| x /= y | x = x / y | divide |
| x %= y | x = x % y | Modulus (remainder of division) |
| a .= b | a = a . b | Concatenate two strings |
The following examples demonstrate the different results obtained using different assignment operators:
Example
Try it yourself »
The following examples demonstrate the same results obtained using different string operators:
Example
Try it yourself »
PHP Increment/Decrement Operators
| Operator | Name | Description |
|---|---|---|
| ++ x | Pre-increment | Adds 1 to x, then returns x |
| x ++ | Post-increment | Returns x, then adds 1 to x |
| -- x | Pre-decrement | Subtracts 1 from x, then returns x |
| x -- | Post-decrement | Returns x, then subtracts 1 from x |
The following examples demonstrate the results obtained using increment/decrement operators:
Example
Try it yourself »
PHP Comparison Operators
Comparison operators allow you to compare two values:
| Operator | Name | Description | Example |
|---|---|---|---|
| x == y | Equal | Returns true if x equals y | 5==8 returns false |
| x === y | Identical | Returns true if x equals y and they are of the same type | 5==="5" returns false |
| x != y | Not equal | Returns true if x does not equal y | 5!=8 returns true |
| x <> y | Not equal | Returns true if x does not equal y | 5<>8 returns true |
| x !== y | Not identical | Returns true if x does not equal y, or if they are not of the same type | 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 examples demonstrate the different results obtained using some comparison operators:
Example
Try it yourself »
PHP Logical Operators
| Operator | Name | Description | Example |
|---|---|---|---|
| x and y | and | Returns true if both x and y are true | x=6 y=3 (x < 10 and y > 1) returns 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 | 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 examples demonstrate the different results obtained using some array operators:
Example
Try it yourself »
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 if expr1 evaluates to TRUE, otherwise returns expr3.
Example
In the following example, by checking whether the $_GET request contains a user value, it returns $_GET['user'] if present, otherwise returns nobody:
Example
Note:PHP_EOLis a newline character, compatible with more platforms.
In PHP 7+ versions, there is an additional NULL coalescing operator??, examples are as follows:
Example
Combined Comparison Operator (PHP7+)
PHP7+ supports the combined comparison operator, also called the spaceship operator, with the symbol:<=>. The combined comparison operator can easily compare two variables, and of course it is not limited to comparison of numeric data.
The syntax format is as follows:
$c = $a <=> $b;
Parsing is as follows:
- If $a > $b, then$cthe value of1。
- If$a == $b, then$cthe value of0。
- If$a < $b, then$cthe value of-1。
Examples are as follows:
Example
Operator Precedence
The following table lists operators in order of precedence from high to low. Operators on the same row have the same precedence, in which case their associativity determines the order of evaluation.
Explanation: Left = left to right, right = 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 operators and string operators |
| Left | << >> | Bitwise operators |
| None | == != === !== <> | Comparison operators |
| Left | & | Bitwise operators and references |
| 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 multiple places |
In operator precedence, or and ||, && and and are all logical operators with the same effect, but their precedence is different.
Example
The output of the above examples is:
int(3) bool(true)
Use of Parentheses
We use parentheses pairing to explicitly indicate the order of operations, rather than relying on operator precedence and associativity, which can usually increase code readability.
Example
The output of the above examples is:
7 9Other extensions