-
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 left-to-right leftist order is used.
The following table lists the precedence of operators from high to low. Operators in the same row have the same precedence, at which point their binding direction determines the order of evaluation.
Operator Precedence
Combination Direction |
operator |
Additional Information |
Non-binding |
Clone new |
Clone and new |
Left |
[ |
Array () |
Non-binding |
++ -- |
Increment/decrement operator |
Non-binding |
~-(int) (float) (string) (array) (object) (BOOL) @ |
Type |
Non-binding |
instanceof |
Type |
Right combination |
! |
logical operators |
Left |
* / % |
Arithmetic operators |
Left |
+ - . |
Arithmetic operators and string operators |
Left |
<< >> |
Bitwise operators |
Non-binding |
< <= > >= <> |
Comparison operators |
Non-binding |
== != === !== |
Comparison operators |
Left |
& |
Bitwise operators and references |
Left |
^ |
Bitwise operators |
Left |
| |
Bitwise operators |
Left |
&& |
logical operators |
Left |
|| |
logical operators |
Left |
? : |
Ternary operators |
Right |
= + = = *=/=. =%= &= |= ^= <<= >>= |
Assignment operators |
Left |
and |
logical operators |
Left |
Xor |
logical operators |
Left |
Or |
logical operators |
Left |
, |
Used in many places |
Leftist indicates that the expression is evaluated from left to right and vice versa.
Example #1 Combination Direction
<?php$a = 3 * 3 5; (3 * 3)% 5 = 4$a = true? 0:true? 1:2; (true? 0:true)? 1:2 = 2$a = 1; $b = 2; $a = $b + = 3; $a = ($b + = 3), $a = 5, $b = 5?> use parentheses to enhance the readability of your code.
Note:
Although = is lower than most other operators, PHP still allows an expression similar to the following:if (! $a = foo ()), in this case foo () The return value is assigned to the $a .
Http://www.docin.com/p1-1560374424.html
Http://www.docin.com/p1-1560374424.html
Introduction to PHP Operator precedence