Operator Precedence
The precedence of an operator specifies how “tightly” it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication (“*”) operator has a higher precedence than the addition (“+”) operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.
When operators have equal precedence, their associativity decides whether they are evaluated starting from the right, or starting from the left – see the examples below.
The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides the order of evaluation.
Associativity | Operators | Additional Information |
---|---|---|
non-associative | clone new | clone and new |
left | [ | array() |
non-associative | ++ — | increment/decrement |
right | ~ – (int) (float) (string) (array) (object) (bool) @ | types |
non-associative | instanceof | types |
right | ! | logical |
left | * / % | arithmetic |
left | + – . | string |
left | << >> | bitwise |
non-associative | < <= > >= <> | comparison |
non-associative | == != === !== | comparison |
left | & | 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 |