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

Null Coalescing Operator Not Used

Style PHP 7.0+ Beginner
debt(d3/e1/b1/t5)
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 linting/fixing tools that ship with rules covering the isset($x) ? $x : $default pattern. This is a well-known style pattern caught by default rulesets in these tools without specialist configuration.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix states replacing isset($x) ? $x : $default with ?? is a direct, mechanical substitution. Tools like rector and php-cs-fixer can automate this at scale, making each individual fix a one-line replacement.

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

Closest to 'minimal commitment' (b1). This is a localised style choice affecting individual expressions. Not using ?? imposes no architectural weight, no cross-cutting burden, and no structural decay — it's a per-expression verbosity issue that doesn't shape system design or slow down future maintainers beyond minor readability friction.

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

Closest to 'notable trap' (t5). The misconception field explicitly states that ?? and ?: are confused as equivalent — but ?: triggers E_NOTICE for undefined variables while ?? suppresses it. This is a documented, well-known gotcha that competent developers often discover the hard way, especially when migrating from PHP 5 patterns. It's a real behavioral difference, not just style, meriting t5.

About DEBT scoring →

Also Known As

?? operator null coalescing ??= operator

TL;DR

Using isset() + ternary or if/else chains when the null coalescing operator (??) or null coalescing assignment (??=) would be cleaner and more idiomatic.

Explanation

PHP 7.0 introduced ?? which returns the left operand if it exists and is not null, otherwise the right. PHP 7.4 added ??= which assigns only if the variable is null. These replace the common isset($x) ? $x : $default pattern. The operator also works on array keys and chained access: $config['db']['host'] ?? 'localhost'. Unlike the ternary shorthand (?:), null coalescing does not trigger a notice for undefined variables.

Common Misconception

?? and ?: are the same — ?: triggers E_NOTICE for undefined variables; ?? suppresses the undefined notice and only checks for null.

Why It Matters

isset($x) ? $x : $default is verbose and error-prone when chaining multiple fallbacks — $a ?? $b ?? $c ?? 'default' is cleaner and correctly handles undefined variables.

Common Mistakes

  • Using isset($x) ? $x : $default when $x ?? $default is available (PHP 7+).
  • if (!isset($arr[$key])) $arr[$key] = []; — use $arr[$key] ??= [].
  • Using ?: which triggers notices for undefined variables instead of ??.
  • Forgetting ?? works on chained array access: $data['user']['name'] ?? 'Unknown'.

Code Examples

✗ Vulnerable
// Verbose — pre-PHP 7 style:
$name    = isset($_GET['name']) ? $_GET['name'] : 'Guest';
$timeout = isset($config['timeout']) ? $config['timeout'] : 30;
$locale  = isset($user) && isset($user->locale) ? $user->locale : 'en';

// Assignment:
if (!isset($cache[$key])) {
    $cache[$key] = [];
}
✓ Fixed
// Clean — null coalescing:
$name    = $_GET['name']       ?? 'Guest';
$timeout = $config['timeout']  ?? 30;
$locale  = $user?->locale      ?? 'en';

// Null coalescing assignment:
$cache[$key] ??= [];

// Chained:
$value = $request->get('x') ?? $config['x'] ?? $defaults['x'] ?? null;

Added 16 Mar 2026
Edited 22 Mar 2026
Views 55
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 0 pings S 0 pings M 0 pings T 1 ping W 2 pings T 2 pings F 0 pings S 0 pings S 3 pings M 1 ping T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 10 Perplexity 6 Scrapy 6 Google 5 Ahrefs 4 ChatGPT 3 Bing 3 Unknown AI 2 SEMrush 2 Claude 1 Meta AI 1 Yandex 1
crawler 40 crawler_json 4
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Replace isset($x) ? $x : $default and array_key_exists + ternary with the ?? operator — it's null-safe, handles undefined array keys without notices, and is chainable
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
isset($x) ? $x : $default pattern; $arr['key'] ? $arr['key'] : 'default' double access; ternary returning same variable as condition
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