OPERATORS IN PHP

php logo

PRECEDENCE AMONG OPERATORS

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 can be used to force precedence if necessary. For example: (1 + 5) * 3 returns 18. When operators have equal precedence, their associativity decides how to group the operators. For example, “-” is associative on the left, so 1 – 2 – 3 is grouped as (1 – 2) – 3 and returns -4. “=” on the other hand is associative right, so $a = $b = $c is grouped as $a = ($b = $c). Equal precedence operators that are not associative cannot be used next to each other, e.g. 1 <= 2 > 1 is illegal in PHP. The expression 1 <= 1 == 1 on the other hand is legal, because the operator == has a lower precedence than the operator <=.

ASSOCIATIVITY

Associativity is meaningful only for binary (and ternary) operators. Unary operators are prefixed or suffixed, so this notion does not apply. For example !!$a can be grouped only as !(!$a). The use of parentheses, even when not strictly necessary, can often increase code readability by making the grouping explicit rather than relying on the precedence and associativity of the implicit operator. The following table lists the operators in order of precedence, with those with the highest precedence at the top. Operators on the same row have equal precedence, in which case associativity decides the grouping.

Precedence
Copy to Clipboard

OPERATORS ON ARRAY CODE

Copy to Clipboard

TERNARY OPERATOR CODE

Copy to Clipboard

NULL OPERATOR COLESCING (PHP 7) CODE

Copy to Clipboard

FURTHER INFORMATION

Associativity in PHP refers to how operators are evaluated in an expression that contains multiple operators of the same type.
Specifically, it determines the direction by which operators are associated and resolved when there are multiple operations that need to be evaluated.

Types of Associativeness

There are two main types of associativity in PHP:

1. Left-to-Right Associativity:

– Operators that follow this associativity are evaluated starting from left to right. For example, arithmetic operators such as +, , *, /, and % are all associative from left to right. If you have an expression such as 5 – 2 + 3, it will be evaluated as (5 – 2) + 3.

<?php

    $result = 5 – 2 + 3; // $result sarà uguale a 6
?>

2. Right-to-Left Associativity:

-In this case, operators are evaluated starting from right to left.
A common example of this type of associativity are assignment operators such as = and unary operators such as ++ and –.
An expression such as $a = $b = 5 is evaluated as $a = ($b = 5).

<?php

$a = $b = 5; // Prima viene valutato $b = 5, poi $a = $b
?>

Associativity of Common Operators in PHP

Arithmetic Operators (+, , *, /, %): Associativity from left to right.

Assignment Operators (=, +=, -=, *=, /=, %=): Associativity from right to left.

-Operators ofComparison (==, !=, ===, !==, >, <, >=, <=): Associativity from left to right.

Logical Operators (&&, ||, and, or, xor): Some have left-to-right associativity (
&&, ||), while others(and, or, xor) depend on priority over other operators.

-Concatenation operator(.): Associativity from left to right.

-Ternary operator(? :): Associativity from right to left.

Detailed Example

Consider a practical example with various operators to see how associativity affects evaluation:

<?php

$result = 10 – 4 + 2 * 3;
// Associatività e precedenza:
// 1. Prima viene valutato 2 * 3 (precedenza più alta), risultato: 6
// 2. Poi 10 – 4 (associatività da sinistra a destra), risultato: 6
// 3. Infine, 6 + 6, risultato finale: 12

echo $result; // Output: 12
?>

Precedence vs. Associativeness

It is important not to confuse operator precedence with associativity.
Precedence determines which operators are evaluated first in an expression, while associativity determines the order in which operators with the same precedence are evaluated.

For example, in the expression 2 + 3 * 4, the operator * has a higher precedence than +, so the operation 3 * 4 is executed first, and only then is 2 + 12 executed.
Associativity comes into play when two operators have the same precedence, determining the order in which they are evaluated.

Conclusion

Understanding associativity is critical to correctly predicting the outcome of complex expressions in PHP.
Keeping in mind how operators are associated and evaluated allows you to write clearer and more unambiguous code, avoiding potential logical errors that could result from misinterpretation of expressions.

LINKS TO PREVIOUS POSTS

THE PHP LANGUAGE

LINK TO THE CODE ON GITHUB

GITHUB