The PHP Ternary Operator: Shorthand Conditionals Done Right
Master the ternary and Elvis operators for clean, readable one-liner conditionals. Learn when to use them and when to step back to if/else.
The PHP Ternary Operator: Shorthand Conditionals Done Right
If you've been writing PHP for any length of time, you've reached for an if/else block when a one-liner would have done the job. The ternary operator — and its shorthand variants — are exactly that. They're not exotic; they're bread-and-butter PHP that you should use confidently.
The Standard Ternary
The classic form:
Concrete example:
The expression evaluates $user->isAdmin(). If truthy, $role gets 'admin'; otherwise 'member'. Clean, readable, one line.
The Shorthand Ternary (Elvis Operator)
PHP 5.3 introduced the shorthand ternary, sometimes called the Elvis operator (?:), which drops the middle operand:
This is equivalent to:
The expression evaluates $value and returns it if truthy; otherwise returns $fallback.
Practical example:
If the request contains a non-empty username, you get it. If it's null, an empty string, or 0, you fall back to 'Guest'. That's the common use case: providing a default when a value might be absent or empty.
The Null Coalescing Operator (??)
PHP 7 added the null coalescing operator, which you should reach for most often in modern PHP:
The key difference from ?: is that ?? only checks for null or an undefined variable/key. It does not treat 0, false, or empty strings as falsy.
This is safe even if $_GET['page'] is not set — no undefined index notice. It also won't discard 0 if that's a legitimate value.
Compare directly:
I've seen bugs introduced by using ?: where ?? was correct, precisely because developers didn't consider whether zero or false were valid values in their domain.
Null Coalescing Assignment (??=)
PHP 7.4 added ??=, a compact assignment shorthand:
Equivalent to:
Useful for setting array or object defaults without verbosity. I use this regularly when building configuration merges or hydrating request data with sensible defaults.
Nesting: Possible, but Be Careful
You can chain ternaries, but PHP's left-associativity historically made nested ternaries behave unexpectedly. Since PHP 8.0, nesting ternaries without explicit parentheses produces a deprecation error that becomes fatal in later versions. Use parentheses if you must nest:
If you're reaching for a second or third nesting level, a proper if/elseif/else or a match expression is almost certainly more readable. Don't sacrifice clarity for brevity — the next engineer reading this code is usually you in six months.
When to Use Which
| Situation | Operator |
|---|---|
| Two distinct outcomes based on condition | ? : |
| Default for any falsy value | ?: (Elvis) |
| Default only for null or undefined | ?? |
| Assign default if key/variable not set | ??= |
| Three or more branches | match or if/else |
On Readability
Ternaries earn their place when the condition and both outcomes fit on one line and the intent is obvious. The moment you're squinting to parse it, write it out properly. Code is read far more often than it is written.
The ?? operator is worth adopting as a default habit in any modern PHP codebase — it's explicit about intent, safe with undefined keys, and reads naturally.
Quick Reference
Pick the right tool for the specific check you're doing, keep it readable, and you'll write cleaner PHP for it.
Damian Hodgkiss
Senior Staff Engineer at Sumo Group, leading development of AppSumo marketplace. Technical solopreneur with 25+ years of experience building SaaS products.