When talking about PHP logical operators, we mentioned the priority of PHP operators. The so-called operator priority refers to which operator is first calculated and then calculated in the expression, the result of expression 1 + 5*3 mentioned the priority of the PHP operator when we talked about the PHP logical operator, it refers to the operator in the expression that is first calculated and then calculated. it is like the result of expression 1 + 5*3.
It is 16 instead of 18 because the multiplication ("*") has a higher priority than the plus sign ("+. If necessary, use parentheses to forcibly change the priority. Example: (1 + 5) the value of * 3 is 18.
The PHP operator follows the rule in the operation: the operation with a higher priority is executed first, and the operation with a lower priority is executed later. if the operation has the same priority, the operation is executed from left to right. for example, "-" is left join, so 1-2-3 is equivalent to (1-
2)-3 and the result is-4. on the other hand, "=" is right join, so $ a = $ B = $ c is equivalent to $ a = ($ B = $ c ).
Operators in parentheses are executed first. even if they are not necessary, parentheses are used to clearly indicate the order of operation, rather than operator priority and associativity, this improves code readability.
The table lists operators from high to low by priority. Operators in the same row have the same priority. in this case, their union direction determines the order of value.
Integration direction |
Operator |
Additional information |
None |
Clone new |
Clone and new < |
Left |
[ |
Array () |
Right |
** |
Arithmetic operators |
Right |
++ -- ~ (Int) (Float) (String) (Array) (Object) (Bool) @ |
Type and increment/decrease |
None |
Instanceof |
Type |
Right |
! |
Logical operators |
Left |
* / % |
Arithmetic operators |
Left |
+ - . |
Arithmetic operators and string operators |
Left |
< > |
Bitwise operators |
None |
< <= > > = |
Comparison Operators |
None |
= ! = === ! = <> <=> |
Comparison Operators |
Left |
& |
Bitwise operators and references < |
Left |
^ |
Bitwise operators |
Left |
| |
Bitwise operators |
Left |
&& |
Logical operators |
Left |
| |
Logical operators |
Left |
?? |
Comparison Operators |
Left |
? : |
Ternary operators |
Right |
= + = -= * = ** = /= . = % = & = | = ^ = <= >>= |
Value assignment operator |
Left |
And |
Logical operators |
Left |
Xor |
Logical operators |
Left |
Or |
Logical operators |
It is unrealistic and unnecessary to remember so many priorities. if the written expressions are complex and contain many operators, use the brackets as follows:
This reduces the possibility of logical errors.
Sometimes parentheses can enhance the readability of the code. For example:
$ A = 5, $ B = 5?>
The above is a detailed explanation of the php operator priority order. For more information, see other related articles in the first PHP community!