: This article mainly introduces the differences in operator priorities between PHP and JavaScript. For more information about PHP tutorials, see. Most of the two have the same priority. comparision and assignment operations have slight differences. Comparison operators include >,<, >=, and so on. value assignment operators include =, + =, and * =.
In JS, the comparison operator has a higher priority than the value assignment operator. So foo = 1 <2 result foo = false;
In PHP, the value assignment operator has a higher priority than the comparison operator. So foo = 1 <2 result foo = 1, and the expression is false. Parentheses must be added to achieve the same result as above.
Foo = (1 <2)
Appendix: complete operator priority of PHP and JavaScript
JS
The following table is ordered from highest (19) to lowest (0) precedence.
Precedence |
Operator type |
Associativity |
Inpidual operators |
19 |
Grouping |
N/ |
( … ) |
18 |
Member Access |
Left-to-right |
… . … |
Computed Member Access |
Left-to-right |
… [ … ] |
New (with argument list) |
N/ |
new … ( … ) |
17 |
Function Call |
Left-to-right |
… ( … ) |
New (without argument list) |
Right-to-left |
new … |
16 |
Postfix Increment |
N/ |
… ++ |
Postfix Decrement |
N/ |
… -- |
15 |
Logical NOT |
Right-to-left |
! … |
Bitwise NOT |
Right-to-left |
~ … |
Unary Plus |
Right-to-left |
+ … |
Unary Negation |
Right-to-left |
- … |
Prefix Increment |
Right-to-left |
++ … |
Prefix Decrement |
Right-to-left |
-- … |
Typeof |
Right-to-left |
typeof … |
Void |
Right-to-left |
void … |
Delete |
Right-to-left |
delete … |
14 |
Exponentiation |
Right-to-left |
… ** … |
Multiplication |
Left-to-right |
… * … |
Division |
Left-to-right |
… / … |
Remainder |
Left-to-right |
… % … |
13 |
Addition |
Left-to-right |
… + … |
Subtraction |
Left-to-right |
… - … |
12 |
Bitwise Left Shift |
Left-to-right |
… << … |
Bitwise Right Shift |
Left-to-right |
… >> … |
Bitwise Unsigned Right Shift |
Left-to-right |
… >>> … |
11 |
Less |
Left-to-right |
… < … |
Less Than Or Equal |
Left-to-right |
… <= … |
Greater |
Left-to-right |
… > … |
Greater Than Or Equal |
Left-to-right |
… >= … |
In |
Left-to-right |
… in … |
Instanceof |
Left-to-right |
… instanceof … |
10 |
Equality |
Left-to-right |
… == … |
Inequality |
Left-to-right |
… != … |
Strict Equality |
Left-to-right |
… === … |
Strict Inequality |
Left-to-right |
… !== … |
9 |
Bitwise AND |
Left-to-right |
… & … |
8 |
Bitwise XOR |
Left-to-right |
… ^ … |
7 |
Bitwise OR |
Left-to-right |
… | … |
6 |
Logical AND |
Left-to-right |
… && … |
5 |
Logical OR |
Left-to-right |
… || … |
4 |
Conditional |
Right-to-left |
… ? … : … |
3 |
Assignment |
Right-to-left |
… = … |
… += … |
… -= … |
… **= … |
… *= … |
… /= … |
… %= … |
… <<= … |
… >>= … |
… >>>= … |
… &= … |
… ^= … |
… |= … |
2 |
Yield |
Right-to-left |
yield … |
1 |
Spread |
N/ |
... ... |
0 |
PHP
Operator Precedence
Associativity |
Operators |
Additional Information |
Non-associative |
Clone new |
Clone andnew |
Left |
[ |
Array () |
Right |
** |
Arithmetic |
Right |
++ --~ (Int) (float) (string) (array) (object) (bool )@ |
Types andincrement/decrement |
Non-associative |
Instanceof |
Types |
Right |
! |
Logical |
Left |
*/% |
Arithmetic |
Left |
+ -. |
Arithmetic andstring |
Left |
<> |
Bitwise |
Non-associative |
<=> = |
Comparison |
Non-associative |
=! ===! ==<><=> |
Comparison |
Left |
& |
Bitwise andreferences |
Left |
^ |
Bitwise |
Left |
| |
Bitwise |
Left |
&& |
Logical |
Left |
| |
Logical |
Right |
?? |
Comparison |
Left |
? : |
Ternary |
Right |
= + =-= * = ** =/=. = % = & = | = ^ = <= >>=> |
Assignment |
Left |
And |
Logical |
Left |
Xor |
Logical |
Left |
Or |
Logical |
|
The above introduces the differences in the operator priorities between PHP and JavaScript, including the content, and hope to be helpful to friends who are interested in PHP tutorials.