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

each() & list() — Deprecated Iteration

php PHP 3.0+ Beginner

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 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 2 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Google 7 Amazonbot 7 Unknown AI 3 Perplexity 3 SEMrush 2 ChatGPT 1 Ahrefs 1
crawler 21 crawler_json 1 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