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

Unused Variable

quality PHP 5.0+ Beginner
debt(d5/e1/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan, psalm, and phpcs — all specialist static-analysis tools that must be deliberately configured (e.g. phpstan level 4+). No PHP compiler or default linter catches this automatically; it requires intentional tooling setup.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix states: enable phpstan level 4+ and phpcs. Once detected, the fix is either deleting the unused variable, correcting a typo in the variable name, or adding a log call — all single-line or trivial changes.

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

Closest to 'localised tax' (b3). The issue is scoped to individual variables within functions or methods. It applies broadly across web, cli, and queue-worker contexts, but each instance is self-contained; it does not impose a cross-cutting architectural weight on the codebase.

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 states that developers treat unused variables as mere style issues, when in reality an unused variable after a function call silently discards a return value that may be an error indicator. This contradicts the intuition that 'the function was called, so its effect happened' — the result (e.g. error signal) is silently lost, causing undetected failures. This is a deeper trap than a documented gotcha.

About DEBT scoring →

Also Known As

unused assignment assigned never read dead variable

TL;DR

A variable that is assigned but never read — indicating a logic error, incomplete refactoring, or unnecessary computation.

Explanation

Unused variables come in two flavours: assigned and never read (possible logic error — was the value supposed to be used?), and loop variables assigned inside a loop but overwritten before being read (result discarded). PHP lacks compile-time unused variable warnings unlike strongly typed languages. PHPStan level 5+ and Psalm detect them. The fix is either to use the variable or to remove the assignment. In foreach, prefix unused variables with underscore by convention: foreach ($items as $_key => $value).

Common Misconception

Unused variables are just style issues — an unused variable after a function call often means the return value (possibly an error indicator) is being silently discarded.

Why It Matters

An unused variable that was supposed to be returned or checked silently discards a result — errors go undetected and callers receive wrong data.

Common Mistakes

  • Assigning a function's return value to a variable then never using it — the return value likely matters.
  • Reassigning a variable in a loop before reading the previous value — the first assignment was wasted.
  • Unused catch variable: catch (Exception $e) {} — at least log $e.
  • Unnecessary intermediate variables that just hold a value passed directly to the next call.

Code Examples

✗ Vulnerable
// Return value assigned but ignored:
$result = $pdo->exec($sql); // 0 means 0 rows affected — never checked

$userId = getUserId(); // Assigned
$user   = findUser();  // $userId never used — logic error?
return $user;

// Overwritten before read:
foreach ($items as $item) {
    $total = 0;        // Reset every iteration — previous value lost
    $total += $item->price;
}
✓ Fixed
// Check return values:
$affected = $pdo->exec($sql);
if ($affected === 0) throw new RuntimeException('No rows updated');

// Use what you assign:
$userId = getUserId();
$user   = findUser($userId); // $userId used
return $user;

// Fix loop — move init outside:
$total = 0;
foreach ($items as $item) {
    $total += $item->price;
}

Added 16 Mar 2026
Edited 5 Apr 2026
Views 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 10 Perplexity 5 Unknown AI 3 Ahrefs 2 ChatGPT 2 Google 1
crawler 21 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Enable phpstan level 4+ and run phpcs with relevant rules to detect unused variables — they're either bugs (wrong variable name) or dead code (assigned but never read)
📦 Applies To
PHP 5.0+ any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
$result = expensive(); // result never used; $user = getUser(); but $usr used later (typo); foreach assigns value not used
Auto-detectable: ✓ Yes phpstan psalm phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Low Context: Function
CWE-563

✓ schema.org compliant