← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Elvis Operator Not Used

Style PHP 5.3+ Beginner
debt(d3/e1/b3/t7)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list rector, php-cs-fixer, and phpcs — all standard PHP code-style tools that ship with rules flagging `$x ? $x : $default` patterns. These are commonly enabled by default or in standard rulesets, placing this squarely at d3.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is a direct single-expression substitution: replace `$x ? $x : 'default'` with `$x ?: 'default'` or `$x ?? 'default'`. Rector can automate this. No structural change needed.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice affects any PHP web, CLI, or queue-worker context (broad applies_to) but each individual misuse is isolated to its expression. It doesn't shape architectural decisions — it's a stylistic tax paid at the call-site level, not across the whole codebase, so b3 fits better than b5.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap (contradicts how a similar concept works elsewhere)' (t7). The misconception field explicitly calls out that ?: and ?? are commonly believed interchangeable but behave differently for falsy values like 0, '', and false. Using ?: instead of ?? for nullable values silently drops valid 0 or false data in production — a documented gotcha that contradicts reasonable developer intuition about 'null-safe' shorthand, warranting t7.

About DEBT scoring →

Also Known As

?: Elvis operator shorthand ternary

TL;DR

Writing $x ? $x : $y instead of $x ?: $y — the Elvis operator (?:) returns the left operand if truthy, otherwise the right, eliminating the repeated expression.

Explanation

PHP 5.3 introduced the shorthand ternary (Elvis) operator: $x ?: $y is equivalent to $x ? $x : $y. It eliminates repeating the condition variable. Note: ?: triggers E_NOTICE for undefined variables — for null checks on potentially undefined keys use ?? instead. Use ?: when the variable is defined and you want the first truthy value; use ?? when the variable may be undefined and you want the first non-null value.

Common Misconception

?: and ?? are interchangeable — ?: returns the left side if it is truthy (so 0, '', false trigger the fallback); ?? returns the left side if it is not null (so 0, '', false do NOT trigger the fallback).

Why It Matters

Using the wrong operator between ?: and ?? for numeric or boolean values silently replaces valid 0 or false with a default — a common source of subtle bugs in form handling.

Common Mistakes

  • $value ? $value : $default when $value ?: $default is cleaner.
  • Using ?: instead of ?? for array keys — $arr['key'] ?: 'default' triggers a notice if 'key' doesn't exist.
  • ?: for nullable values where 0 or '' should not trigger the fallback — use ?? instead.
  • Chaining ternaries without using ?: making the intent unclear.

Code Examples

✗ Vulnerable
// Repetition — same variable twice:
$title       = $post->title    ? $post->title    : 'Untitled';
$displayName = $user->name     ? $user->name     : 'Anonymous';
$port        = $config['port'] ? $config['port'] : 8080;
// Also: $port uses ?: which replaces 0 — bug if port 0 is valid
✓ Fixed
// Elvis operator — no repetition:
$title       = $post->title ?: 'Untitled';
$displayName = $user->name  ?: 'Anonymous';

// But: use ?? when value may be null or undefined:
$port = $config['port'] ?? 8080; // 0 is a valid port — use ??

// Correct pairing:
// ?: for 'first truthy value' (0/false/'' trigger fallback)
// ?? for 'first non-null value' (0/false/'' do NOT trigger fallback)

Added 16 Mar 2026
Edited 22 Mar 2026
Views 54
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 2 pings M 2 pings T 2 pings W 1 ping T 2 pings F 1 ping S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 6 ChatGPT 6 Scrapy 5 Ahrefs 4 SEMrush 3 Unknown AI 2 Google 2 Bing 2 Majestic 1 Claude 1 Meta AI 1 Yandex 1 PetalBot 1
crawler 39 crawler_json 3
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use $x ?: 'default' (Elvis operator) for falsy fallbacks or $x ?? 'default' for null-only fallbacks — the Elvis operator is the ternary without the middle operand
📦 Applies To
PHP 5.3+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
$x ? $x : 'default' that could be $x ?: 'default'; not using shorthand ternary where available
Auto-detectable: ✓ Yes rector php-cs-fixer phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant