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

each() & list() — Deprecated Iteration

PHP PHP 3.0+ Beginner
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The term's detection_hints lists rector and phpcs — both are specialist static analysis / code-transformation tools, not default linters. The metadata also notes 'static analysis tools miss it in some dynamic call patterns,' confirming it is not caught universally or instantly, placing it firmly at d5.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is a direct pattern replacement (while(list($k,$v)=each($arr)) → foreach($arr as $k => $v)) automatable via Rector. It stays within one file per occurrence but may require touching many files across a legacy codebase — still a mechanical find-and-replace pattern, so e3 is accurate rather than e5.

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

Closest to 'localised tax' (b3). The concept applies to web and cli contexts in legacy PHP code, but it is a deprecated iteration pattern, not a load-bearing architectural choice. It imposes a tax on upgrade work (finding all call sites before PHP 8 migration) but does not shape unrelated parts of the codebase or impose ongoing maintenance weight once migrated.

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

Closest to 'notable trap' (t5). The misconception field explicitly states that developers commonly believe list() was also removed along with each() — but list() and [] destructuring remain fully supported. This is a documented, widely-encountered gotcha during PHP 7→8 upgrades that most developers stumble on, earning t5.

About DEBT scoring →

TL;DR

each() was deprecated in PHP 7.2 and removed in PHP 8 — replace while(list($k,$v)=each($arr)) with foreach. list() itself is still valid as [] destructuring.

Explanation

each($array) returned the current key-value pair and advanced the pointer — deprecated PHP 7.2, removed PHP 8. The while(list($k,$v)=each($arr)) idiom was a PHP 3/4 pattern for iteration. Modern replacement: foreach($arr as $k => $v). list() itself (PHP 5) and its short form [] (PHP 7.1) remain valid for array destructuring: [$a, $b] = [1, 2]. Rector auto-converts each() usage. Also removed in PHP 8: reset()/current()/next() in foreach-able contexts are still fine but each() is gone.

Common Misconception

list() was removed with each() — list() and [] destructuring are still fully supported. Only each() was removed.

Why It Matters

each() was removed in PHP 8 — code using it throws a fatal error with no graceful fallback. It was commonly used in legacy codebases for while(list($k,$v) = each($arr)) iteration patterns. Finding and replacing all occurrences before upgrading is critical because static analysis tools miss it in some dynamic call patterns. The modern equivalent is foreach with list() or array destructuring.

Common Mistakes

  • Confusing list() removal (not removed) with each() removal (removed in PHP 8).
  • Not running Rector to auto-migrate each() calls.
  • Missing that reset() is still needed if starting from first element in non-foreach context.

Code Examples

✗ Vulnerable
// Removed in PHP 8:
reset($users);
while (list($key, $user) = each($users)) {
    echo $key . ': ' . $user['name'];
}
✓ Fixed
// Modern:
foreach ($users as $key => $user) {
    echo $key . ': ' . $user['name'];
}

// list() / [] still valid:
[$first, $second] = $coords;
list($name, $email) = $row;

Added 23 Mar 2026
Views 47
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
Sogou 1
Google 7 Amazonbot 7 SEMrush 4 Unknown AI 3 Perplexity 3 Ahrefs 3 Scrapy 3 Claude 2 ChatGPT 1 Bing 1 Meta AI 1 Majestic 1 Sogou 1
crawler 32 crawler_json 3 pre-tracking 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Replace while(list($k,$v)=each($arr)) with foreach($arr as $k => $v). Run Rector to automate. list() and [] remain valid for destructuring.
📦 Applies To
PHP 3.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
each\(\$
Auto-detectable: ✓ Yes rector phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant