Precedence and Associativity
Explore how PHP evaluates expressions by understanding operator precedence and associativity. Learn which operators execute first and how expressions with operators of equal precedence are handled. This knowledge helps you write clearer PHP code and avoid logic errors. Using brackets is recommended to enhance readability and clarify evaluation order.
We'll cover the following...
Precedence #
Operator precedence determines which operator is performed first in an expression with more than one operators. Operator precedence in PHP is similar to that of regular arithmetic operators.
*,/,%operators have equal precedence.+,-operators have equal precedence.*,/,%have a higher precedence than+,-.
The operator with higher precedence is executed first in the expression.
Associativity #
Associativity is used when two operators of the same precedence appear in an expression.
Left Associativity #
Left associativity occurs when an expression is evaluated from left to right. For example * and / have the same precedence and their associativity is left to right, so the expression ...