PHP Shorthand if

Su Subhalakshmi Chandran Updated 17 Oct 2026
8 min read ·Lesson 40 of 102
In this post

"Shorthand if" refers to compact ways of writing simple decisions: Form Syntax Purpose Ternary operator condition ? valueIfTrue : valueIfFalse Choose between two values Short ternary ("Elvis") value ?

"Shorthand if" refers to compact ways of writing simple decisions:

FormSyntaxPurpose
Ternary operatorcondition ? valueIfTrue : valueIfFalseChoose between two values
Short ternary ("Elvis")value ?: fallbackUse a value unless it is falsy
Null coalescingvalue ?? fallbackUse a value unless it is null or missing
One-line ifif (condition) statement;Run one statement (not recommended)

The most important of these is the ternary operator, which this lesson covers in depth. The null coalescing operator has its own lesson (Null Coalescing Operator).


The Ternary Operator ?:

What It Is

In simple terms: The ternary operator is a one-line if...else that produces a value: "if this is true, use A; otherwise, use B".

Technically: ?: is the only operator in PHP with three operands (hence ternary). It evaluates the condition, then evaluates and returns only one of the other two expressions.

Syntax

$result = condition ? valueIfTrue : valueIfFalse;

Example

<?php
$age = 20;

$label = $age >= 18 ? 'Adult' : 'Minor';

echo $label;

Output

Adult

Equivalent if...else

<?php
$age = 20;

if ($age >= 18) {
    $label = 'Adult';
} else {
    $label = 'Minor';
}

echo $label;

Both versions produce the same result. The ternary version is shorter and keeps the assignment on one line, which makes it ideal when the only thing each branch does is produce a value.


Why the Ternary Operator Is an Expression

if...else is a statement: it controls which code runs. The ternary operator is an expression: it produces a value. That means it can be used anywhere a value is expected — in assignments, function arguments, return statements, string concatenation, and array definitions.

Example: Using the Result Directly

<?php
function itemCount(int $count): string
{
    return $count . ' ' . ($count === 1 ? 'item' : 'items');
}

echo itemCount(1) . "\n";
echo itemCount(3) . "\n";

$user = ['name' => 'Vimal', 'premium' => true];

$profile = [
    'name' => $user['name'],
    'badge' => $user['premium'] ? 'Premium member' : 'Free member',
    'storageGb' => $user['premium'] ? 100 : 5,
];

print_r($profile);

Output

1 item
3 items
Array
(
    [name] => Vimal
    [badge] => Premium member
    [storageGb] => 100
)

Singular/plural labels, CSS classes, badges, and limits are typical uses: small decisions embedded in a larger expression.


Ternary in HTML Templates

The ternary operator works well inside <?= ?> output tags:

<?php
$tasks = [
    ['title' => 'Write lesson', 'done' => true],
    ['title' => 'Record video', 'done' => false],
];
?>
<ul>
<?php foreach ($tasks as $task): ?>
    <li class="<?= $task['done'] ? 'done' : 'pending' ?>"><?= htmlspecialchars($task['title']) ?> <?= $task['done'] ? '✔' : '' ?></li>
<?php endforeach; ?>
</ul>

Output (HTML)

<ul>
    <li class="done">Write lesson ✔</li>
    <li class="pending">Record video </li>
</ul>

The ternary chooses the CSS class and an optional check mark for each task.


Precedence: Use Parentheses

The ternary operator has low precedence — lower than concatenation (.), arithmetic, and comparisons. When it appears inside a larger expression, wrap it in parentheses.

The Concatenation Trap

<?php
$isActive = false;

echo "Status: " . $isActive ? 'Active' : 'Inactive';
echo "\n";
echo "Status: " . ($isActive ? 'Active' : 'Inactive');
echo "\n";

Output

Active
Status: Inactive

Why This Happens

In the first line, . is evaluated first, so PHP reads it as:

("Status: " . $isActive) ? 'Active' : 'Inactive'

"Status: " . false is the string "Status: ", which is truthy, so the result is 'Active' — and the label text is lost entirely. The parentheses in the second line make the ternary a single unit.

Rule: whenever a ternary is part of a larger expression, put it in parentheses.


Nested Ternaries

A ternary can contain another ternary, but since PHP 8.0, nesting without parentheses is a fatal error:

<?php
$score = 75;

// Fatal error in PHP 8:
// echo $score >= 90 ? 'A' : $score >= 75 ? 'B' : 'C';

echo $score >= 90 ? 'A' : ($score >= 75 ? 'B' : 'C');

Output

B

Even with parentheses, nested ternaries quickly become hard to read. For three or more outcomes, use match (true):

<?php
$score = 75;

$grade = match (true) {
    $score >= 90 => 'A',
    $score >= 75 => 'B',
    default => 'C',
};

echo $grade;

Output

B

Each outcome is on its own line, which makes the logic easy to scan and extend. See PHP Match.


The Short Ternary ?: (Elvis Operator)

What It Is

If you leave out the middle part of a ternary, $a ?: $b returns $a when it is truthy, otherwise $b. It is shorthand for $a ? $a : $b, but $a is evaluated only once.

The name "Elvis operator" comes from ?: resembling an emoticon with Elvis Presley's hairstyle.

Example

<?php
$nickname = '';
$title = 'Weekly Report';
$items = [];

echo ($nickname ?: 'Anonymous') . "\n";
echo ($title ?: 'Untitled') . "\n";
echo count($items ?: ['(no items)']) . "\n";

Output

Anonymous
Weekly Report
1
  • '' is falsy, so 'Anonymous' is used.
  • 'Weekly Report' is truthy, so it is kept.
  • [] is falsy, so the fallback array with one element is used.

?: vs ??

Value of $x$x ?: 'default'$x ?? 'default'
null'default''default'
(undefined)'default' plus a warning'default'
'''default'''
0'default'0
'0''default''0'
'PHP''PHP''PHP'
  • Use ?: when any empty value should be replaced, and the variable is known to exist.
  • Use ?? when only missing or null values should be replaced, and 0, '', or '0' are valid.

Combining Both

<?php
$query = ['q' => '   '];

$search = trim($query['q'] ?? '') ?: 'all products';

echo "Showing results for: $search\n";

Output

Showing results for: all products

?? safely handles a missing q parameter, trim() removes the spaces, and ?: replaces the resulting empty string.


One-Line if Without Braces

PHP allows an if with a single statement on one line:

<?php
$debug = true;

if ($debug) echo "Debug mode is on\n";

Output

Debug mode is on

This is valid, but it is easy to break: adding a second statement later does not make it part of the if. Most coding standards (including PER Coding Style) require braces for every control structure. Use the braced form:

if ($debug) {
    echo "Debug mode is on\n";
}

When Not to Use the Ternary Operator

For Side Effects Instead of Values

// Legal, but unclear
$isValid ? saveOrder($order) : logError($order);

The ternary operator is designed to produce a value. Using it only to call one of two functions hides control flow inside an expression. Use if...else:

if ($isValid) {
    saveOrder($order);
} else {
    logError($order);
}

For Long or Complex Conditions

If the condition or the values do not fit comfortably on one line, the ternary saves no effort and costs readability. Use if...else, match, or extract the condition into a well-named variable first.


Practical Example: Formatting an Order Summary

<?php
function orderSummary(array $order): string
{
    $itemWord = $order['items'] === 1 ? 'item' : 'items';
    $shipping = $order['total'] >= 999 ? 'FREE' : '₹79';
    $note = trim($order['note'] ?? '') ?: 'No delivery note';
    $priority = $order['express'] ? ' [EXPRESS]' : '';

    return sprintf(
        "Order #%d%s: %d %s, total ₹%s, shipping %s. Note: %s",
        $order['id'],
        $priority,
        $order['items'],
        $itemWord,
        number_format($order['total']),
        $shipping,
        $note
    );
}

echo orderSummary(['id' => 501, 'items' => 1, 'total' => 450, 'express' => false]) . "\n";
echo orderSummary(['id' => 502, 'items' => 3, 'total' => 2400, 'express' => true, 'note' => 'Leave at gate']) . "\n";

Output

Order #501: 1 item, total ₹450, shipping ₹79. Note: No delivery note
Order #502 [EXPRESS]: 3 items, total ₹2,400, shipping FREE. Note: Leave at gate

Step-by-Step Explanation

  1. $itemWord chooses the singular or plural word.
  2. $shipping chooses a label based on the order total.
  3. $note combines ?? (the key may be missing) with ?: (an empty note should be replaced).
  4. $priority adds a marker only for express orders; an empty string means "add nothing".
  5. Each small decision is stored in a well-named variable, so the final sprintf() stays simple.

Common Mistakes

Missing Parentheses Inside Expressions

Effect: "Status: " . $x ? 'A' : 'B' loses the label and evaluates the wrong condition.

Fix: "Status: " . ($x ? 'A' : 'B').

Unparenthesized Nested Ternaries

Effect: Fatal error in PHP 8.

Fix: Add parentheses, or better, use match (true) or if...elseif.

Using ?: on Possibly Undefined Values

Effect: "Undefined variable" or "Undefined array key" warnings.

Fix: Use ?? for values that may not exist, or combine: ($x ?? '') ?: 'default'.

Using ?: When 0 Is Valid

Effect: A quantity of 0 is replaced by the default.

Fix: Use ?? or an explicit comparison.

Using the Ternary for Side Effects

Effect: Hidden control flow that is hard to read and debug.

Fix: Use if...else.


Best Practices

  • Use the ternary operator for short choices between two values.
  • Always wrap a ternary in parentheses when it is part of a larger expression.
  • Prefer match (true) or if...elseif for three or more outcomes.
  • Use ?? for missing or null values, and ?: for empty values that are known to exist.
  • Always use braces with if, even for one statement.

Official reference: https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Home PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement PHP Home PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope PHP Casting Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence Continue Statement Modify Strings PHP If...Else...Elseif PHP If Modify Strings PHP If Operators PHP Functions Concatenate Strings PHP If...Else Slicing Strings User-defined Functions Slicing Strings PHP Shorthand if Escape Characters Default Argument Values PHP Nested if Pass by Reference String Interpolation Named Arguments PHP File Upload Variadic Functions Return Values Search in Strings Multibyte Strings Type Declarations in Functions PHP Numbers PHP Math PHP Floats PHP Random Numbers PHP Constants PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Cookies PHP Destructor PHP Access Modifiers Readonly Properties PHP Sessions PHP Inheritance Method Overriding PHP Filters The final Keyword CSRF Protection PHP Filters Advanced Session Security Secure Cookies PHP Magic Constants File Upload Security PHP Callback Functions PHP Operators Filesystem Security PHP JSON Arithmetic Operators Error Reporting in Production Hiding PHP Assignment Operators PHP Configuration (php.ini) Comparison Operators PHP Command Line (CLI) PHP Exceptions Increment / Decrement Operators Migrating to PHP 8 PHP Form Validation PHP Form Required PHP Form URL/E-mail Error Handling Form Input Sanitizing Escaping Output (htmlspecialchars) Custom Error Handler Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting
Ask about this post
AI Ask about this post

Ask questions about PHP Shorthand if and get answers drawn from it.

Signed-in readers only.