PHP - Operators Precedence
PHP Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
Operator Associativity
Operator associativity is the direction in which an expression is evaluated. For example:
$x = 10; $y = 20; //associativity of = operator is //Left to Right, hence x will become 20 $x = $y;
As the associativity of = is left to right. Hence x is assigned the value of y.
Example:
The example below illustrates the operator precedence in PHP.
<?php //evaluates 5 * 2 first $retval1 = 15 - 5 * 2; //above expression is equivalent to $retval2 = 15 - (5 * 2); //forcing compiler to evaluate 15 - 5 first $retval3 = (15 - 5) * 2; echo "15 - 5 * 2 = $retval1 \n"; echo "15 - (5 * 2) = $retval2 \n"; echo "(15 - 5) * 2 = $retval3 \n"; ?>
The output of the above code will be:
15 - 5 * 2 = 5 15 - (5 * 2) = 5 (15 - 5) * 2 = 20
PHP Operators Precedence Table
The following table lists the precedence and associativity of PHP operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.
Precedence | Operator | Description | Associativity |
---|---|---|---|
25 | clone | clone operator | NA |
new | new operator | ||
24 | ** | Exponentiation | Right to Left |
23 | ++a --a | Prefix increment, Prefix decrement | NA |
a++ a-- | Postfix increment, Postfix decrement | ||
+a -a | Unary plus, Unary minus | ||
~ | Bitwise NOT | ||
(int) (float) (string) (array) (object) (bool) | Type casting | ||
@ | Error control operator | ||
22 | instanceof | Type operator | Left to Right |
21 | ! | Logical NOT | NA |
20 | * / % | Multiplication, Division, Remainder | Left to Right |
19 | + - . | Addition, Subtraction, Concatenating operator (. prior to PHP 8.0.0) | |
18 | << >> | Bitwise left shift and right shift | |
17 | . | Concatenating operator (as of PHP 8.0.0) | |
16 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | non-associative |
15 | == != | Equality, Inequality | |
=== !== | Strict Equality, Strict Inequality | ||
<> <=> | Inequality, Spaceship operator | ||
14 | & | Bitwise AND and reference operator | Left to Right |
13 | ^ | Bitwise XOR | |
12 | | | Bitwise OR | |
11 | && | Logical AND | |
10 | || | Logical OR | |
9 | ?? | Null coalescing operator | Right to Left |
8 | a?b:c | Conditional (ternary) operator | Non-associative (Left to Right associative prior to PHP 8.0.0) |
7 | = | Direct assignment | Right to Left |
+= -= *= /= %= **= | Compound assignment by addition, subtraction, multiplication, division, remainder, and exponentiation | ||
.= ??= | Concatenating assignment operator, Null coalescing assignment operator | ||
<<= >>= | Compound assignment by Bitwise left shift and right shift | ||
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||
6 | yield from | yield from | NA |
5 | yield | yield | |
4 | |||
3 | and | Logical AND | Left to Right |
2 | xor | Logical XOR | |
1 | or | Logical OR |
❮ PHP - Operators