Operator Precedence
Like in math: 3 + 4 * 5 = 23 and not 35, in PHP you have an order for interpreting the code.
Associativity | Operators | Additional Information |
---|---|---|
non-associative | clone new | clone and new |
left | [ | array() |
right | ++ — ~ (int) (float) (string) (array) (object) (bool) @ | types and increment/decrement |
non-associative | instanceof | types |
right | ! | logical |
left | * / % | arithmetic |
left | + – . | arithmetic ?i string |
left | << >> | bitwise |
non-associative | < <= > >= | comparison |
non-associative | == != === !== <> | comparison |
left | & | bitwise ?i references |
left | ^ | bitwise |
left | | | bitwise |
left | && | logical |
left | || | logical |
left | ? : | ternary |
right | = += -= *= /= .= %= &= |= ^= <<= >>= => | assignment |
left | and | logical |
left | xor | logical |
left | or | logical |
left | , | many uses |
For operators that have the same precedence, the order is specified in the first column.
PHP also knows about Asociativity.
Exception:
if (!$a = foo())
Even if = has lower precedence than other operators (! here) the return falue of foo() is put into $a before the negation