PHP Shorthand If
In this page:
$result = condition ? value_if_true : value_if_false;
$result = $value ?: $fallback;
$result = $value ?? $fallback;
The Basic Ternary Operator
condition ? valueIfTrue : valueIfFalse evaluates condition, and returns valueIfTrue if it is truthy or valueIfFalse otherwise -- compressing a four-line if-else block that only assigns a value into a single line.
Example: The Basic Ternary Operator
<?php
// Declare `$age`, set to `20`
$age = 20;
// Print `$age >= 18 ? "Adult" : "Minor"` to the output
echo $age >= 18 ? "Adult" : "Minor";
?>
Login to try C/C++/Java/PHP code in the editor
The Shorthand Ternary (Elvis Operator)
condition ?: valueIfFalse is a shorter form that skips repeating the middle value -- it returns condition itself if condition is truthy, or valueIfFalse if condition is falsy.
This is especially handy for picking a variable's own value if it is truthy, or a fallback otherwise.
Example: The Shorthand Ternary (Elvis Operator)
<?php
// Declare `$name`, set to ""
$name = "";
// Print `$name ?: "Anonymous"` to the output
echo $name ?: "Anonymous";
?>
Login to try C/C++/Java/PHP code in the editor
The Null Coalescing Operator (??)
value ?? fallback returns fallback only if value is null or entirely undefined (an unset variable or missing array key), without raising a warning for the undefined case -- unlike the shorthand ternary, it does not trigger on other falsy values like 0, "", or false.
Example: The Null Coalescing Operator (??)
<?php
// Declare `$count`, set to `0`
$count = 0;
// Print `$count ?? "no value"` to the output
echo $count ?? "no value";
?>
Login to try C/C++/Java/PHP code in the editor
The Null Coalescing Assignment Operator (??=)
$variable ??= $fallback assigns $fallback to $variable only if $variable is currently null or unset, leaving it untouched otherwise -- a compact way to "fill in a default only if missing" without an explicit if statement.
Example: The Null Coalescing Assignment Operator (??=)
<?php
// Declare `$config` as an empty array
$config = [];
$config['theme'] ??= "light";
// Print `$config['theme']` to the output
echo $config['theme'];
?>
Login to try C/C++/Java/PHP code in the editor
When to Avoid Shorthand If
Shorthand forms are great for a single value decision, but once a branch needs to run more than one statement, log something, or call multiple functions, a regular if-else block is clearer and easier to maintain -- shorthand syntax should make code shorter without making it harder to understand.
Example: When to Avoid Shorthand If
<?php
// Declare `$isAdmin`, set to `true`
$isAdmin = true;
// Check whether `$isAdmin`
if ($isAdmin) {
// Print "Logging admin access\n" to the output
echo "Logging admin access\n";
// Print "Welcome, admin!" to the output
echo "Welcome, admin!";
// Otherwise, run this branch
} else {
// Print "Access denied" to the output
echo "Access denied";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Overusing the ternary operator for genuinely complex logic, producing a dense, hard-to-read one-liner where a normal if-else block would actually be clearer.
- Confusing the null coalescing operator (??) with the ternary operator -- ?? specifically checks for null/unset, while ?: checks general truthiness.
- Chaining multiple ternary operators without parentheses, which can produce confusing or unintended grouping.
- The ternary operator, condition ? valueIfTrue : valueIfFalse, compresses a simple if-else into one expression.
- The shorthand ternary, condition ?: valueIfFalse, returns condition itself when it is truthy, skipping the repeated middle value.
- The null coalescing operator, value ?? fallback, returns fallback specifically when value is null or undefined, which is subtly different from general truthiness.
The ternary operator has been in PHP since its earliest versions; the null coalescing operator (??) was introduced in PHP 7.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: