When we talked about the PHP logical operators, we mentioned the precedence of the PHP operators, the so-called operator precedence, which is the calculation of which operation Mr. Foo in the expression, which is calculated as if the result of the expression 1 + 5 * 3 is 16 instead of 18 because multiplication sign ("*") has precedence over the plus sign ("+ ") high. If necessary, you can use parentheses to force a change in precedence. For example: (1 + 5) * 3 has a value of 18. The rules that are followed in the operation of the PHP operators are: High priority operations are performed first, followed by low priority operations, and if the same priority is performed in left-to-right order, 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). The operators in 1486188938564607.gif parentheses are executed first, 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, can improve the readability of the code. The 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. Append information with direction operator no clone Newclone and new< left [array () Right * * arithmetic operator right + +-~ (int) (float) (string) (array) (object) (BOOL) @ Type and increment/decrement no instanceof type right! Logical operator Left */% arithmetic operator left +-. Arithmetic operator and string operator left << >> bitwise operator None < <= > >= comparison operator None = = = = =!== & Lt;> <=> comparison operator left & bitwise operator and Reference < left ^ bitwise operator LEFT | bitwise operator LEFT && logical operator left | | Logical operator left?? Comparison operator left? : ternary operator right = + = = *= **=/=. =%= &= |= ^= <<= >>= assignment operator left and logical operator left XOR logical operator left or logical operator so many priority levels, if you want to remember very clearly, is not too realistic, nor It is necessary to write expressions that are complex and contain more run-in characters, and do not use parentheses, like this: <?php$a and (($b! = $c) or (5* (50-$d)); > SoYou can reduce the likelihood that a logic error may occur sometimes using parentheses can enhance the readability of your code. For example, the following: <?php$a = 3 * 3 5; (3 * 3)% 5 = 4//ternary run combined $ 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?> above is the details of the order of precedence of PHP operators, more attention to the PHP Chinese web other related articles!
PHP operator Precedence