The precedence of an operator specifies how "tightly" it binds two expressions together. The operator precedence is as follows:
Precedence | Operator Mark | Operators |
---|---|---|
High Low |
[ ( | Parenthesis |
++ -- ~ (int) (string) (bool) | Types/Increment/Decrement | |
! | Logical | |
* / % | Arithmetic | |
+ - . | Arithmetic | |
<< >> | Bitwise | |
< <= > >= | Comparison | |
== != === !== <> | Comparison | |
& | Bitwise | |
^ | Bitwise | |
| | Bitwise | |
&& | Logical | |
|| | Logical | |
? : | Ternary | |
= += -= *= /= .= %= &= |= ^= <<= >>= | Assignment |
When operators which have the same priority are used repeatedly, calculation is started from the left. However, for the assignment, increment/decrement, cast and logical operator'!', the operation starts from the right.
<?php
$var0 = 3 * 3 % 5; // (3 * 3) % 5 = 4 (from left)
$var1 = 1;
$var2 = 2;
$var1 = $var2 += 3; // $var1 = ($var2 += 3), $var1, $var2 = 5 (from right)
?>