Ternary and Ternary Coalescing Operator in PHP
PHP supports various forms of ternary and coalescing operators. This is a quick post to catch-up to all ternary and coalescing operators supports in PHP.
1. Ternary Operator: cond ? expr1 : expr2
Ternary operator is a short form for an if/else
block that executes exactly one expression each.
if ($marks >= 50) {
$rank = 'pass';
}
else {
$rank = 'fail';
}
The if/else
block above is quite verbose and consumes more space for a simple expression. With the ternary operator, you can optimize this:
- if ($marks >= 50) {
- $rank = 'pass';
- }
- else {
- $rank = 'fail';
- }
+ $rank = $marks >= 50 ? 'pass' : 'fail';
Its syntax is follows:
result = condition ? expression-if-true : expression-if-false
PHP will execute condition
condition, and if it evaluates to "true-ish" value (same semantics as an if()
condition), value from expression-if-true
is used. If condition
evaluates to false, value from expression-if-false
will be used.
An ideal use case of Ternary operator would be assignments that can have two values.
From PHP 8.0+, you can also throw
an exception from a Ternary operator.
2. Shorthand Ternary Operator: cond ?: else-expr
Certain Ternary operator expressions can be simplified further. Consider the following Ternary expression:
$user = load_user() ? load_user() : false;
If the conditional expression is the same as the true-expression, it is possible to reduce this further:
- $user = load_user() ? load_user() : false;
+ $user = load_user() ?: false;
If load_user()
function returns a false-ish value, $user
will be assigned false
. Otherwise, the return value of load_user()
will be assigned.
Its syntax is:
result = cond ?: expression-if-false
The cond
condition will be evaluated as if it's in an if()
block, and the return value will be assigned to result
if it is a truthy value. If it's a false-ish value (such as 0
, "0"
, false
, null
, []
, etc), the expression-if-false
expression will be evaluated, and its return value will be assigned to result
.
3. Null Coalescing Operator
Null Coalescing Operator provides a shorthand for isset()
calls. It is often used to reduce excessive isset()
calls. Null Coalescing operator calls isset()
on the conditional expression, and the value will be returned.
- $result = isset($_GET['value']) ? $_GET['value'] : 'foo';
+ $result = $_GET['value'] ?? 'foo';
If $_GET['value']
is set (which behaves exactly the way isset()
does), $_GET['value']
value will be assigned to $result
. If it is not set, or null
, foo
will be assigned to $result
.
result = variable ?? expression
PHP calls isset(variable)
, and variable
will be assigned to result
if variable
is set. If it is not set, expression
will be evaluated, and its value will be assigned to result
.
4. Null Coalescing Assignment operator
Null Coalescing operator can be be further reduced in PHP 7.4, with Null Coalescing Assignment operator .
- $value = $value ?? 'foo';
+ $value ??= 'foo';
A word of caution
Be mindful when you chain ternary/coalescing operators. It is now required to use braces to make the intent clear if you absolutely have to use ternary/coalescing operators.
Null Coalescing Assignment operator is relatively new in PHP (added in PHP 7.4), so you code might not work in older PHP versions if you decide to use that operator.
These operators are syntax sugar only, and do not provide any meaningful performance difference compared to good ol' if/else
blocks. When the intent is not clear, it is recommended to go with if/else
blocks although they make the code slightly verbose