Last updated: April 10, 2026
Over 290,000 developers have searched for the difference between PHP’s Elvis operator and the PHP null coalescing operator – and most of them were mid-refactor, staring at a codebase that predates PHP 7. That is not a niche problem. It is a near-universal rite of passage for any team modernising a PHP application, and getting it wrong silently breaks things in production. Here is what you actually need to know.
The Elvis Operator (?:): Fast, Loose, and Occasionally Treacherous

Image: Stack Overflow
The Elvis operator is the right tool when you want to fall back on a default if a value is falsy – and you know the variable is already defined.
The Elvis operator (?:) is shorthand for $var ? $var : 'default'. It evaluates the left-hand side using PHP’s loose comparison rules and returns it if truthy, otherwise returns the right-hand side. Simple enough. But “loose comparison” is where developers get burnt. PHP considers empty strings, zero, false, and empty arrays all falsy. So $count ?: 0 does not protect a zero value – it replaces it with zero, which looks correct until $count legitimately is zero and you wanted to preserve that.
The bigger trap: accessing an undefined variable or a missing array key with ?: triggers an E_NOTICE – an “Undefined variable” or “Undefined index” warning. These notices are suppressed in many production environments, so the code appears to work. It does not. You are silently swallowing a signal that something is wrong, accumulating technical debt with every request. Three consequences follow: your logs are polluted, your debugging is harder, and your production errors become invisible.
The PHP Null Coalescing Operator (??): Safer by Design
The null coalescing operator (??) does one thing and does it well: it returns the left-hand side if it exists and is not null, otherwise it returns the right-hand side.
Introduced in PHP 7.0 specifically to replace the verbose isset($var) ? $var : 'default' pattern, ?? is the language team saying: “we know you write this constantly, so here is a first-class operator for it.” The critical difference from ?: is that ?? only falls through on null or an unset variable. It does not care about falsy values. Zero passes through. An empty string passes through. false passes through. If you want to preserve those values, ?? is the correct tool.
Accessing an undefined array key is where ?? really earns its keep. $_GET['page'] ?? 1 returns 1 if the key does not exist – silently, cleanly, without a notice. The same expression with ?: triggers an “Undefined index” notice every time the key is absent. For high-traffic applications processing thousands of requests, that is thousands of unnecessary notices per second.
PHP 7.4 went further with the null coalescing assignment operator (??=). The pattern $var = $var ?? 'default' collapses to $var ??= 'default'. Concise, readable, and expressive – the kind of small improvement that compounds across a large codebase.
Head-to-Head: Where They Actually Diverge
Both operators produce the same result when the left-hand side is null. The divergence is everywhere else.
Think of it like a bouncer at a door. The Elvis operator (?:) turns away anyone who looks suspicious – empty pockets, no shoes, wearing black. Zero, empty string, false: all turned away. The null coalescing operator (??) has one rule: no ghosts. If you exist and you are not null, you are in. That analogy is not just decorative – it is the mental model that prevents the most common bugs when swapping between the two.
Here is the before/after that matters in practice:
Before (PHP 5 legacy pattern):
$page = isset($_GET['page']) ? $_GET['page'] : 1;
After (PHP 7+ with null coalescing):
$page = $_GET['page'] ?? 1;
Before (preserving zero):
$qty = $input['quantity'] ?: 0; – broken, replaces zero with zero but also hides the issue.
After:
$qty = $input['quantity'] ?? 0; – correct, preserves zero values, suppresses undefined index notices.
The question of which framework you are building on matters too. Whether you are working in PHP via WordPress (see this breakdown of WooCommerce Pricing 2026: Hosting, Plugins & Real Costs for the real cost picture) or a Python-based stack – the Django CMS vs WordPress: which one should you choose? comparison covers that decision well – the language-level operator choices shape how robust your defaults are.
When to Pick Each: A Straight Answer
Use ?? by default when accessing variables that might not exist – query parameters, array keys, config values, session data. It is safer, quieter, and expresses your intent clearly: “I want this value unless it is absent.”
Use ?: when the variable is guaranteed to exist and you want to replace any falsy value – not just null. Replacing an empty username with 'Anonymous', for instance. $display = $username ?: 'Anonymous' is correct there. ?? would pass through an empty string, which is probably not what you want.
Never use ?: on array keys or superglobals ($_GET, $_POST, $_SESSION) in PHP 7+. There is no justification. The notice suppression alone makes ?? the correct choice every time. Migrating a legacy codebase? Do a global search for ?: on any line accessing an array and replace methodically. Your error logs will thank you.
DRS Web Development builds custom websites and web applications for businesses of all sizes – whether you need a clean PHP migration, a robust WordPress build, or something built from scratch. Get in touch for a free consultation at drs-web.co.uk/contact.
Frequently Asked Questions
Q: What is the difference between the Elvis operator and the null coalescing operator in PHP?
A: The Elvis operator (?:) returns the left-hand side if it is truthy, falling through on zero, false, empty strings, and empty arrays. The null coalescing operator (??) returns the left-hand side if it exists and is not null, passing through zero, false, and empty strings. Use ?? when accessing potentially undefined variables; use ?: when you want to replace any falsy value.
Q: Does the PHP null coalescing operator suppress undefined variable notices?
A: Yes. Unlike ?:, the ?? operator does not trigger an E_NOTICE for undefined variables or missing array keys. It silently returns the fallback value, making it safe for use with superglobals like $_GET and $_POST.
Q: When was the null coalescing operator introduced in PHP?
A: The null coalescing operator (??) was introduced in PHP 7.0 as a first-class language feature specifically to replace the verbose isset($var) ? $var : 'default' pattern. PHP 7.4 added the shorthand assignment form ??=.
Q: Can the null coalescing operator replace the Elvis operator entirely?
A: No. If you need to replace falsy values like empty strings or zero – not just null – the Elvis operator (?:) is the correct tool. The two operators have meaningfully different behaviour, and choosing between them depends on whether you care about falsy values or only about null and undefined.
Q: Is the Elvis operator safe to use on array keys in PHP 7+?
A: No. Accessing a missing array key with ?: triggers an “Undefined index” notice. Use ?? instead – it handles missing keys cleanly without notices and is the idiomatic PHP 7+ approach.
This article was researched and written with AI assistance, then reviewed for accuracy and quality. Riya Shah uses AI tools to help produce content faster while maintaining editorial standards.
Need help with your web project?
From one-day launches to full-scale builds, DRS Web Development delivers modern, fast websites.





