Elvis vs Null Coalescing: What's the Catch? (Under the Hood of PHP)
\n\nHello, 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 ??.
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\nBrief Introduction: Why Do We Even Need These Operators?
\n\nImagine you're writing code to display a user's name. If there's no name — show "Guest". The classic way is if:
if (isset($userName)) {\n $displayName = $userName;\n} else {\n $displayName = 'Guest';\n}\n\nIt 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.
1. Elvis Operator (?:) — "The King of Boolean Values"
\n\nThe 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.
Syntax:
\n$result = $expression ?: $default;\n\nWhat 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.
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\nExample with a Catch:
\n$count = 0;\n$result = $count ?: 10;\necho $result; // Outputs 10, not 0!\n\nWhy? Because 0 is a "falsy" value. Elvis considered the variable "empty" and returned the fallback. For many, this comes as a surprise.
2. Null Coalescing Operator (??) — "The Savior from Notice"
\n\nThe 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\nSyntax:
\n$result = $expression ?? $default;\n\nWhat does this mean? The operator checks if $expression exists and is not equal to null. If so — it returns $expression. Otherwise — it returns $default.
Key difference: ?? ignores all "falsy" values except null and undefined. 0, "", false — all of these are considered valid values and will be returned.
Example:
\n$count = 0;\n$result = $count ?? 10;\necho $result; // Outputs 0! Because the variable exists and is not null.\n\nSee the difference? This is the main "under the hood" mechanism.
\n\n3. Comparison "Under the Hood": Truth Table
\n\nLet's summarize everything in a clear table to solidify the concept. Let