PHP Operator Precedence

What is Operator Precedence?

Explanation

The precedence of an operator is important to bind two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the "*" operator has a higher precedence than the "+" operator.

Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18. If operator precedence is equal, left to right associativity is used.

The following table lists the precedence of operators with the highest-precedence operators listed at the top of the table.
Associativity Operators Additional Information
non-associative clone new clone and new
left [ array()
non-associative ++ -- increment/decrement
non-associative ~ - (int) (float) (string) (array) (object) (bool) @ types
non-associative instanceof types
right ! Logical
left * / % arithmetic
left + - . arithmetic and string
left << >> bitwise
non-associative < <= > >= <> comparison
non-associative == != === !== comparison
left & bitwise and 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

Left associativity means that the expression is evaluated from left to right, right associativity means from right to left.

PHP Topics


Ask Questions

Ask Question