When to Use PHP’s ?: vs ?? Operators: A Complete Guide

This article explains the differences between PHP’s ternary (?:) and null‑coalescing (??) operators, shows their syntax, key behaviors, practical examples, comparison table, chaining usage, and guidance on choosing the right operator for robust backend code.

php Courses
php Courses
php Courses
When to Use PHP’s ?: vs ?? Operators: A Complete Guide

Understanding the two operators

In PHP you often need to provide a default value when a variable may be empty. The ternary shorthand ?: and the null‑coalescing operator ?? both help, but they behave differently.

1. Ternary shorthand ?:

Equivalent to $result = $a ? $a : $default;. It evaluates $a in a boolean context; if the value is truthy it is returned, otherwise $default is used. $result = $a ?: $default; Key points

Truthiness includes true, any non‑zero number, non‑empty string, non‑empty array, etc.

Falsy values ( 0, 0.0, "", '0', false, null, undefined variable, empty array) cause the default to be used.

Using an undefined variable triggers an E_NOTICE warning.

Example

$username = $_POST['username'] ?: 'Anonymous User'; // uses posted value if set and non‑empty
$count = 0;
echo $count ?: 'No data'; // outputs "No data" because 0 is falsy

2. Null‑coalescing operator ??

Introduced in PHP 7. Syntax: $result = $a ?? $default;. It checks whether $a is set and not null. If so, it returns $a; otherwise it returns $default. Other falsy values are considered valid. $result = $a ?? $default; Key points

No notice is raised for undefined variables.

Only null or an undefined variable trigger the default.

Values such as 0, false, "" are returned unchanged.

Example

// $_GET['page'] is not set
$page = $_GET['page'] ?? 1; // $page becomes 1

$count = 0;
echo $count ?? 'No data'; // outputs 0

$name = null;
$user = $name ?? 'Unknown User'; // $user becomes "Unknown User"

Core comparison

Evaluation criteria : ?: uses truthiness; ?? uses “isset and not null”.

Handling of null : Both fall back to the default, but ?? does so only for null or undefined.

Handling of 0 , empty string, false : ?: treats them as falsy and uses the default; ?? returns them.

Undefined variables : ?: raises E_NOTICE; ?? is safe.

PHP version : ?: works from PHP 5.3+; ?? requires PHP 7.0+.

Practical scenarios

Use ?? when reading from superglobals or any source where the key may be missing:

$page = $_GET['page'] ?? 1;
$searchTerm = $_POST['keyword'] ?? '';

Use ?? for values that may be null from functions or database calls:

$userEmail = $user->getEmail() ?? '[email protected]';

Use ?: when you want to treat any falsy value as “empty”, for example configuration flags:

$shouldDisplay = $config['display_errors'] ?: false;

Chaining

Null‑coalescing chain returns the first defined, non‑null value: $result = $a ?? $b ?? $c ?? 'final-default'; Ternary shorthand chain returns the first truthy value:

$result = $a ?: $b ?: $c ?: 'final-default';

Conclusion

In modern PHP code, ?? is generally safer for handling user input, object properties, or any data that may be missing, because it preserves legitimate falsy values such as 0 or false. Use ?: only when you intentionally want to replace all falsy values with a default.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backendPHPnull coalescingternary
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.