{
    "slug": "php5_each_function",
    "term": "each() & list() — Deprecated Iteration",
    "category": "php",
    "difficulty": "beginner",
    "short": "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.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "php",
        "history",
        "deprecated",
        "php7",
        "php8",
        "each"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php5_spl_introduction",
        "php_deprecation_notices",
        "php7_performance_leap",
        "rector_automated"
    ],
    "prerequisites": [
        "php5_oop_model"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.each.php"
    ],
    "bad_code": "// Removed in PHP 8:\nreset($users);\nwhile (list($key, $user) = each($users)) {\n    echo $key . ': ' . $user['name'];\n}",
    "good_code": "// Modern:\nforeach ($users as $key => $user) {\n    echo $key . ': ' . $user['name'];\n}\n\n// list() / [] still valid:\n[$first, $second] = $coords;\nlist($name, $email) = $row;",
    "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.",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php5_each_function",
        "html_url": "https://codeclaritylab.com/glossary/php5_each_function",
        "json_url": "https://codeclaritylab.com/glossary/php5_each_function.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[each() & list() — Deprecated Iteration](https://codeclaritylab.com/glossary/php5_each_function) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php5_each_function"
            }
        }
    }
}