Elvis vs Null Coalescing: The Secret Weapon of a PHP Developer

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Elvis vs Null Coalescing: What's the Catch? (Under the Hood of PHP)

\n\n

Hello, colleague! If you've ever written PHP, you've definitely encountered a situation where you need to check if a variable exists, and if so — use its value, and if not — substitute something by default. It seems like a simple task, but this is exactly where beginners (and not only) often stumble over two very similar, yet fundamentally different operators: short-ternary (Elvis operator) ?: and null coalescing operator ??.

\n\n

This is one of the most popular questions on Stack Overflow (thousands of views and votes). And for good reason: the difference between them is not just an academic interest. An error in choosing the operator can lead to bugs that you'll spend hours tracking down. Let's finally figure out what's "under the hood" and when to use which.

\n\n

Brief Introduction: Why Do We Even Need These Operators?

\n\n

Imagine you're writing code to display a user's name. If there's no name — show "Guest". The classic way is if:

\n\n
if (isset($userName)) {\n    $displayName = $userName;\n} else {\n    $displayName = 'Guest';\n}
\n\n

It works, but it's verbose. PHP offers elegant shortcuts. And here we come to the main dilemma: ?: or ??? At first glance — the same thing. But that's only at first glance.

\n\n

1. Elvis Operator (?:) — "The King of Boolean Values"

\n\n

The Elvis operator (nicknamed for its resemblance to the rock 'n' roll king's hairstyle: ?: — eyes, nose, and bangs) appeared in PHP back in version 5.3. It's a shortened form of the ternary operator.

\n\n

Syntax:

\n
$result = $expression ?: $default;
\n\n

What does this mean? The interpreter looks at $expression. If it evaluates to true (i.e., it is not a "falsy" value), then $expression is returned. If $expression is a "falsy" value (false, 0, empty string, null, empty array, etc.), then $default is returned.

\n\n

Important nuance: The Elvis operator does not check whether a variable exists. It checks its truthiness. If the variable is not defined, you will get a Notice: Undefined index/variable.

\n\n

Example with a Catch:

\n
$count = 0;\n$result = $count ?: 10;\necho $result; // Outputs 10, not 0!
\n\n

Why? Because 0 is a "falsy" value. Elvis considered the variable "empty" and returned the fallback. For many, this comes as a surprise.

\n\n

2. Null Coalescing Operator (??) — "The Savior from Notice"

\n\n

The null coalescing operator appeared in PHP 7.0 and was met with enthusiasm. Its main superpower is that it safely works with non-existent variables without causing errors.

\n\n

Syntax:

\n
$result = $expression ?? $default;
\n\n

What does this mean? The operator checks if $expression exists and is not equal to null. If so — it returns $expression. Otherwise — it returns $default.

\n\n

Key difference: ?? ignores all "falsy" values except null and undefined. 0, "", false — all of these are considered valid values and will be returned.

\n\n

Example:

\n
$count = 0;\n$result = $count ?? 10;\necho $result; // Outputs 0! Because the variable exists and is not null.
\n\n

See the difference? This is the main "under the hood" mechanism.

\n\n

3. Comparison "Under the Hood": Truth Table

\n\n

Let's summarize everything in a clear table to solidify the concept. Let

Blogs

Book Recommendations