When working with expressions involving multiple operators in PHP, understanding operator precedence and associativity is crucial. These concepts determine how expressions are evaluated and the order in which different operations are performed.
In this tutorial, we will explore operator precedence and associativity in PHP 8, explaining how they affect the outcome of expressions. We'll cover common operators and their behavior, with examples to make these concepts clear.
What is Operator Precedence?
Operator precedence refers to the rules that govern the order in which different operators are evaluated in a complex expression. Operators with higher precedence are evaluated before operators with lower precedence.
For instance, consider this expression:
$result = 5 + 2 * 3;
Here, the multiplication (*
) operator has a higher precedence than the addition (+
) operator. Thus, PHP first evaluates 2 * 3
, resulting in 6
, and then adds 5
, resulting in 11
.
If we want the addition to happen first, we can use parentheses to explicitly define the order of operations:
$result = (5 + 2) * 3; // Now the result is 21
What is Operator Associativity?
Operator associativity defines the direction in which operators of the same precedence level are evaluated. Associativity can be left-to-right (left-associative) or right-to-left (right-associative).
For example, the addition (+
) and subtraction (-
) operators are left-associative:
$result = 10 - 5 - 2;
In this case, PHP evaluates from left to right: (10 - 5)
gives 5
, and then 5 - 2
gives 3
.
On the other hand, the assignment (=
) operator is right-associative:
$a = $b = $c = 10;
Here, PHP evaluates from right to left: $c = 10
, then $b = $c
, and finally $a = $b
.
PHP 8 Operator Precedence & Associativity Table
The following table lists the most common operators in PHP 8, organized by precedence (from highest to lowest) and their associativity.
Let’s break down a few common operator categories and their precedence and associativity in more detail.
1. Arithmetic Operators (+
, -
, *
, /
, %
)
Arithmetic operators follow standard mathematical rules. Multiplication, division, and modulus (*
, /
, %
) have higher precedence than addition and subtraction (+
, -
).
Example:
$result = 5 + 3 * 2; // Output: 11
This evaluates 3 * 2
first, then adds 5
.
If you want the addition to be performed first:
$result = (5 + 3) * 2; // Output: 16
2. Exponentiation Operator (**
)
The exponentiation operator (**
) has a higher precedence than multiplication and is right-associative.
Example:
$result = 2 ** 3 ** 2; // Output: 512
3. String Concatenation Operator (.
)
The concatenation operator (.
) joins two strings. It has the same precedence as addition and subtraction, and it is left-associative.
Example:
$str = "Hello" . " " . "World"; // Output: "Hello World"
Here, the strings are concatenated from left to right: first "Hello" . " "
and then the result concatenated with "World"
.
4. Comparison Operators (==
, !=
, <
, >
, <=
, >=
, <=>
)
Comparison operators are used to compare values. They have higher precedence than logical operators but lower precedence than arithmetic operators.
Example:
$result = 10 > 5 && 8 < 3; // Output: false
The comparison operators >
and <
are evaluated before the logical &&
operator.
5. Logical Operators (&&
, ||
, and
, or
)
Logical operators like &&
and ||
are used to combine multiple conditions. The and
and or
operators have lower precedence than &&
and ||
, so they are evaluated later.
Example:
$result = true && false || true; // Output: true
Here, &&
is evaluated first (true && false
), resulting in false
. Then false || true
is evaluated, giving true
.
Using and
and or
:
$result = true and false; // Output: true
This is because =
has higher precedence than and
, so it is interpreted as (result = true) and false
.
6. Assignment Operators (=
, +=
, -=
, *=
, /=
, etc.)
Assignment operators are right-associative, meaning they are evaluated from right to left.
Example:
$a = $b = 5;
7. Null Coalescing Operator (??
)
The null coalescing operator (??
) checks if a value exists and is not null
. It is left-associative.
Example:
$name = $userName ?? "Guest";
This means: if $userName
is not set or is null
, assign "Guest"
to $name
.
8. Ternary Operator (?:
)
The ternary operator is a shorthand for an if-else
condition and is right-associative. In PHP 8, the ternary operator does not require the middle part (:
), making it a shorthand for the null coalescing operator.
Example:
$result = $x > 10 ? "Greater" : "Smaller";
If $x > 10
is true, it returns "Greater"
, otherwise "Smaller"
.
9. Bitwise Operators (&
, |
, ^
, ~
, <<
, >>
)
Bitwise operators work on the binary representation of numbers. They follow left-to-right associativity.
Example:
$result = 6 & 3; // Output: 2
This performs a bitwise AND operation on 6
(110
in binary) and 3
(011
in binary), resulting in 010
, which is 2
in decimal.
10. Error Control Operator (@
)
The @
operator suppresses error messages. It has the lowest precedence, meaning it affects the entire expression following it.
Example:
@file_get_contents('non_existent_file.txt');
This prevents any errors from being displayed if the file is not found.
Summary of Key Points
- Operator precedence defines the order in which operations are evaluated.
- Associativity determines the direction of evaluation for operators of the same precedence level.
- Most operators are left-associative, meaning they are evaluated from left to right. Some, like the assignment and ternary operators, are right-associative.
- Parentheses can be used to override default precedence and enforce the desired order of evaluation.
Hope you understand Operator precedence and its Associativity.