在一个表达前运算符为2个以上的情况,决定运算符顺序的为运算符优先顺序。在PHPoC中的运算符优先顺序如下。
优先顺序 | 运算符记号 | 运算符区分 |
---|---|---|
高 低 |
[ ( | Parenthesis |
++ == ~ (int) (string) (bool) | Types/Increment/Decrement | |
! | Logical | |
* / % | Arithmetic | |
+ - . | Arithmetic | |
<< >> | Bitwise | |
< <= > >= | Comparison | |
== != === !== <> | Comparison | |
& | Bitwise | |
^ | Bitwise | |
| | Bitwise | |
&& | Logical | |
|| | Logical | |
? : | Ternary | |
= += -= *= /= .= %= &= |= ^= <<= >>= | Assignment |
※ 当优先顺序相同的运算符重复使用的情况,自左边项开始计算。但,分配、递增/递减、cast、逻辑运算符从右边开始。
<?php
$var0 = 3 * 3 % 5; // (3 * 3) % 5 = 4 (from left)
$var1 = 1;
$var2 = 2;
$var1 = $var2 += 3; // $var1 = ($var2 += 3), $var1, $var2 = 5 (from right)
?>