Operator Precedence in PHP
In this tutorial, we’ll learn about the operator precedence and associativity in PHP with important examples. In PHP, expressions are in general evaluated from left to right.
However, when an expression contains more than one operator, operator precedence determines the order of evaluation. For example, the expression 3 + 5 * 2 evaluates to 13, not to 16, because the multiplication operator (*) has a higher precedence than the additional operator (+). Therefore, it is evaluated first.
However, you can use parentheses to specify the precedence, so that (3 + 5) * 2 evaluates to 16. Thus, when operators of different precedence appear in an expression, the PHP will prioritize the operators first with a higher precedence as compared to a lower precedence to ensure the correct evaluation order.
The operators with higher precedence will evaluate first, regardless of their position in the expression. In PHP, all the operators ordered according to precedence. Therefore, understanding operator precedence helps you to write more readable and efficient code.
What is Operator Associativity?
When the operators in an expression have the same (or equal) precedence, their associativity determines the order in which they are evaluated.
Basically, associativity is the direction in which an expression is evaluated when more than one operator of the equal precedence appears in an expression. Associativity can be:
- Left-associative (left-to-right): Operators are evaluated from left to right.
- Right-associative (right-to-left): Operators are evaluated from right to left.
For example, the assignment operator (=) has right-to-left associativity, while most arithmetic operators have left-to-right associativity.
Precedence and Associativity Table
Here is a table showing the precedence and associativity of PHP operators. Operators listed at the top in the precedence hierarchy have higher precedence than those lower down. When operators are on the same line and have equal precedence, their associativity decides the order in which they are evaluated.
Precedence Level | Operators | Associativity |
---|---|---|
1. | ! , ~ , ++ , -- | Right to Left |
2. | ** | Right to Left |
3. | * , / , % | Left to Right |
4. | + , - , . | Left to Right |
5. | << , >> | Left to Right |
6. | < , <= , > , >= | Left to Right |
7. | == , != , === , !== | Left to Right |
8. | & | Left to Right |
9. | ^ | Left to Right |
10. | | | Left to Right |
11. | && | Left to Right |
12. | || | Left to Right |
13. | ?? | Left to Right |
14. | ?: | Right to Left |
15. | = , += , -= , *= , /= , .= , &= , |= , ^= , ~= | Right to Left |
16. | and | Left to Right |
17. | xor | Left to Right |
18. | or | Left to Right |
Examples of Operator Precedence in PHP
Let’s take some important examples to understand how operator precedence and associativity work in PHP.
Example 1: Arithmetic Operators
<?php
$result = 30 + 50 * 20;
echo $result;
?>
Output: 1030
Here, the multiplication operator (*) has a higher precedence than the addition operator (+), so the sub-expression 50 * 20 is evaluated first, resulting in 1000, and then the sub-expression 30 + 1000 gives 1030.
Example 2: Parentheses to Change Order
<?php
$result = (30 + 50) * 20;
echo $result;
?>
Output: 1600
In this example, we have used the parentheses in the expression. Remember that any operation inside the parentheses is evaluated first. Therefore, the sub-expression (30 + 50) is evaluated first, resulting in 80, and then the sub-expression 80 * 20 gives 1600.
Example 3: Concatenation with Addition
<?php
$result = "Scientech " . "Easy " . 2024 + 1;
echo $result;
?>
Output: Scientech Easy 2025
In this example, the concatenation operator (.) is used to concatenate strings. Therefore, the (.) operator combines two strings “Scientech” . “Easy”, resulting a new string “Scientech Easy”.
Since the concatenation operator (.) has a lower precedence than the addition operator (+), the sub-expression 2024 + 1 is evaluated first, which results 2025. After evaluating 2024 + 1 to 2025, the concatenation operator combines “Scientech Easy ” with 2025. The final result is the string “Scientech Easy 2025”.
The addition operator (+) has higher precedence than the concatenation operator (.). So, the numeric calculation happens before the concatenation operation.
Note that PHP performs automatic type conversion where needed. Here, the integer 2025 is converted to a string when concatenated with “Scientech Easy “.
Example 4: Comparison Operators
<?php
var_dump(3 + 5 > 4 + 2);
?>
Output: bool(true)
In this example, both addition operators have higher precedence than >, so the sub-expressions 3 + 5 and 4 + 2 are evaluated first, yielding 8 > 6, which is true.
Example 5: Logical Operators
<?php
$a = true || false && false;
var_dump($a);
?>
Output: bool(true)
Here, the && operator has a higher precedence than || operator. Therefore, false && false is evaluated first, resulting in false, and then true || false is evaluated as true.
Example 6:
<?php
$price = 120;
$discount = 15;
$taxRate = 0.05;
$finalPrice = $price - $discount + ($price * $taxRate);
echo $finalPrice;
?>
Output: 111
Key Points for Operator Precedence
Operator precedence and associativity in PHP are important to understand for writing correct and efficient code. Here, we have listed some key points:
- Operators having higher precedence are evaluated before operators with lower precedence.
- Operators having the same precedence level are evaluated either from left to right or from right to left, depending on their associativity.
- Try to use parentheses to control the evaluation order explicitly.