PHP 运算符优先级
phpserver side programmingprogramming更新于 2025/8/19 11:07:17
简介
运算符的优先级决定了表达式中运算符的执行顺序。例如,在 2+6/3 中,先进行 6/3 的除法运算,然后再进行 2+2 的加法运算,因为除法运算符 / 的优先级高于加法运算符 +。为了强制某个运算符先于其他运算符调用,应使用括号。在本例中,(2+6)/3 先执行加法运算,然后执行除法运算。
某些运算符的优先级可能相同。在这种情况下,结合性(左或右)决定了运算的顺序。优先级相同但不结合的运算符不能并排使用。下表列出了优先级依次递减的 PHP 运算符。
| Operators | purpose |
| clone new | clone and new |
| ** | exponentiation |
| ++ -- | increment/decrement |
| ~(int) (float) (string) (array) (object) (bool) | casting |
| instanceof | types |
| ! | logical |
| * / | multiplication/division |
| % | modulo |
| + - . | arithmetic and string |
| << >> | bitwise shift |
| < <= > >= | comparison |
| == != === !== <> <=> | comparison |
| & | bitwise and/references |
| ^ | bitwise XOR |
| | | bitwise OR |
| && | logical and |
| || | logical or |
| ?? | null coalescing |
| ? : | ternary |
| = += -= *= **= /= .= %= &= |= ^= <<= >>= ??= | assignment operators |
| yield from | yield from |
| yield | yield |
| and | logical |
| xor | logical |
| or | logical |

