← Back to PHP Course | Chapter 4: Control Flow | Lesson 4 of 14

PHP Shorthand If

A full if-else block takes several lines even for a simple choice, like picking between two short values to assign to a variable. PHP offers two compact shorthand forms for exactly this situation: the ternary operator (condition ? valueIfTrue : valueIfFalse) and the null coalescing operator (value ?? fallback), both of which express a small decision in a single line.

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.

Note: Reach for the ternary operator specifically when both branches simply assign or return a value, not when either branch needs to run multiple statements.

Warning: A ternary operator used for anything beyond a simple value choice quickly becomes harder to read than an equivalent if-else block.

Example: The Basic Ternary Operator

php
<?php
$age = 20;
echo $age >= 18 ? "Adult" : "Minor";
?>

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.

Note: Use the shorthand ternary specifically when the "true" result you want is simply the condition's own value, avoiding the repetition of writing it twice.

Warning: The shorthand ternary checks general truthiness (0, "", and false all count as falsy), which can trigger the fallback in cases where you only meant to handle null or unset.

Example: The Shorthand Ternary (Elvis Operator)

php
<?php
$name = "";
echo $name ?: "Anonymous";
?>

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.

Note: Use ?? specifically for "give me a default if this is missing or null" logic, like reading an optional array key or query parameter safely.

Warning: Confusing ?? with ?: is a common mistake -- ?? treats 0, "", and false as valid, present values, while ?: treats them as falsy and falls back anyway.

Example: The Null Coalescing Operator (??)

php
<?php
$count = 0;
echo $count ?? "no value";
?>

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.

Note: Use ??= to set a default configuration value or initialize a variable only the first time it is touched, without overwriting an already-set value.

Warning: ??= was introduced in PHP 7.4; using it in code that must support older PHP versions will cause a syntax error.

Example: The Null Coalescing Assignment Operator (??=)

php
<?php
$config = [];
$config['theme'] ??= "light";
echo $config['theme'];
?>

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.

Note: If you find yourself nesting ternary operators inside other ternary operators, that is usually a sign to switch back to a normal if-elseif-else block.

Warning: A deeply nested or chained ternary expression can silently produce the wrong grouping without parentheses, since chained ternaries are notoriously easy to misread.

Example: When to Avoid Shorthand If

php
<?php
$isAdmin = true;
if ($isAdmin) {
    echo "Logging admin access\n";
    echo "Welcome, admin!";
} else {
    echo "Access denied";
}
?>
Common Mistakes
  1. 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.
  2. Confusing the null coalescing operator (??) with the ternary operator -- ?? specifically checks for null/unset, while ?: checks general truthiness.
  3. Chaining multiple ternary operators without parentheses, which can produce confusing or unintended grouping.
Chapter Summary
  • 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.
Browser Support

The ternary operator has been in PHP since its earliest versions; the null coalescing operator (??) was introduced in PHP 7.

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.