PHP Ternary Operator
In this page:
$result = condition ? value_if_true : value_if_false;
$result = $value ?: $fallback; // short ternary
$result = $value ?? $fallback; // null coalescing
Basic Ternary Operator
condition ? valueIfTrue : valueIfFalse packs a simple two-way choice into a single expression, replacing a 4-line if/else block that only assigns one variable.
It reads best when the condition and both outcomes are short and clear — for anything more involved, a real if/else stays more readable.
Example: Basic Ternary Operator
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$status`, set to `$age >= 18 ? "adult" : "minor"`
$status = $age >= 18 ? "adult" : "minor";
// Print `$status` to the output
echo $status;
?>
Login to try C/C++/Java/PHP code in the editor
Short Ternary Operator
$value ?: $fallback — the short ternary, sometimes called the Elvis operator for how it looks — returns $value itself if it's truthy, or $fallback otherwise. It's shorthand for $value ? $value : $fallback without repeating $value twice.
Example: Short Ternary Operator
<?php
// Declare `$value`, set to ""
$value = "";
// Declare `$result`, set to `$value ?: "fallback"`
$result = $value ?: "fallback";
// Print `$result` to the output
echo $result;
?>
Login to try C/C++/Java/PHP code in the editor
Null Coalescing Operator
$value ?? $fallback, the null coalescing operator, returns $value unless it's null or entirely undefined, in which case it returns $fallback.
Unlike the short ternary above, it checks specifically for null/undefined rather than any falsy value, so 0 or '' pass through untouched instead of triggering the fallback.
Example: Null Coalescing Operator
<?php
// Declare `$value`, set to `0`
$value = 0;
// Print `$value ?? "fallback"` to the output
echo $value ?? "fallback";
?>
Login to try C/C++/Java/PHP code in the editor
Direct Output with Ternary
Because the ternary operator is itself an expression with a value, you can drop it directly inside an echo call — echo $loggedIn ? 'Welcome back' : 'Please log in'; — without needing a separate variable to hold the chosen string first.
Example: Direct Output with Ternary
<?php
// Declare `$loggedIn`, set to `true`
$loggedIn = true;
// Print `$loggedIn ? 'Welcome back' : 'Please log in'` to the output
echo $loggedIn ? 'Welcome back' : 'Please log in';
?>
Login to try C/C++/Java/PHP code in the editor
Nested Ternary Operators
Nesting one ternary inside another lets you express a short chain of choices in a single line, but readability drops fast past two levels.
Past that point, a match expression or a plain if/elseif chain almost always communicates the same logic more clearly.
Example: Nested Ternary Operators
<?php
// Declare `$score`, set to `75`
$score = 75;
// Declare `$grade`, set to `$score >= 90 ? "A" : ($score >= 70 ? "B" : "C")`
$grade = $score >= 90 ? "A" : ($score >= 70 ? "B" : "C");
// Print `$grade` to the output
echo $grade;
?>
Login to try C/C++/Java/PHP code in the editor
- Nesting ternaries without parentheses, like
$a ? x : $b ? y : z, which is a fatal error in PHP 8. - Using
?:when0or""is a valid value, since the short ternary treats them as falsy and takes the fallback. - Using
??to catch an empty string, when it only falls back fornullor an undefined variable.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: