The operator precedence specifies how tightly the two expressions are bound. For example, the result of the expression 1 + 5 * 3 is that it is because multiplication sign ("*") has a higher priority than the plus sign ("+"). You can use parentheses to force a change of precedence if necessary. For example:(1 + 5) * 3 is the value of.
If the operator has the same precedence, the union direction of the operator determines how it is to be calculated. For example, "-" is leftist, then 1-2-3 is equivalent to (1-2)-3 and the result is -4. On the other hand, "=" is right-linked, so $a = $b = $c equals $a = ($b = $c).
Operators that do not combine the same precedence can not be used together, for example, 1 < 2 > 1 is not valid in PHP. But on the other hand the expression 1 <= 1 = = 1 is legal, because = = priority is lower than <=.
The use of parentheses, even if not necessary, by pairing the parentheses to clearly indicate the order of operations, rather than by operator precedence and binding to determine, usually can increase the readability of the code.
The following table lists the operators by priority from high to low. Operators in the same row have the same precedence, at which point their binding direction determines the order of evaluation.
Combination Direction |
operator |
Additional Information |
No |
Clone new |
Clone and New |
Left |
[ |
Array () |
Right |
** |
Arithmetic operators |
Right |
++ -- ~ (int) (float) (String) (Array) (object) (BOOL) @ |
Type and increment/decrement |
No |
instanceof |
Type |
Right |
! |
logical operators |
Left |
* / % |
Arithmetic operators |
Left |
+ - . |
Arithmetic operators and string operators |
Left |
<< >> |
Bitwise operators |
No |
< <= > >= |
Comparison operators |
No |
== != === !== <> <=> |
Comparison operators |
Left |
& |
Bitwise operators and references |
Left |
^ |
Bitwise operators |
Left |
| |
Bitwise operators |
Left |
&& |
logical operators |
Left |
|| |
logical operators |
Left |
?? |
Comparison operators |
Left |
? : |
Ternary |
Right |
= += -= *= **= /= .= %= &= |= ^= <<= >>= |
Assignment operators |
Left |
and |
logical operators |
Left |
Xor |
logical operators |
Left |
Or |
logical operators |
Operator Precedence in PHP