← Back to PHP Course | Chapter 3: Operators | Lesson 7 of 8

PHP Ternary Operator

The ternary operator is a one-line 'this or that' choice, like asking 'is it sunny? then wear sunglasses, otherwise take an umbrella'. It squeezes a small if-else into a single short expression.
Syntax
php
$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
<?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;
?>

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
<?php
// Declare `$value`, set to ""
$value = "";
// Declare `$result`, set to `$value ?: "fallback"`
$result = $value ?: "fallback";
// Print `$result` to the output
echo $result;
?>

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
<?php
// Declare `$value`, set to `0`
$value = 0;
// Print `$value ?? "fallback"` to the output
echo $value ?? "fallback";
?>

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
<?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';
?>

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
<?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;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Nesting ternaries without parentheses, like $a ? x : $b ? y : z, which is a fatal error in PHP 8.
  2. Using ?: when 0 or "" is a valid value, since the short ternary treats them as falsy and takes the fallback.
  3. Using ?? to catch an empty string, when it only falls back for null or an undefined variable.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.