Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Replacing each() in code that stopped working on PHP 8

ErrorFatal error: Uncaught Error: Call to undefined function each()

Short answer

each() no longer exists in PHP 8.0 and later, so any call ends in a fatal error. Replace the usual while (list($k, $v) = each($a)) loop with foreach. If the code really moves the array pointer by hand, use key(), current() and next() instead. Third-party code that still calls each() needs an updated version.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every call in your code and in vendor code so you know what to change and what to update.

    grep -rn "each(" --include=*.php . | grep -v foreach
  2. 2Match what you see, then jump to its fix

    1. 1while (list($key, $value) = each($array))A while loop that reads an array with each()Fix
    2. 2reset(), next() or prev() are called around each()Code that needs control over the internal array pointerFix
    3. 3The fatal error points at a file under vendor/ or a plugins folderA library or plugin that still calls each()Fix

What does the error look like?

  • Fatal error: Uncaught Error: Call to undefined function each()
  • while (list($key, $value) = each($array)) { ... }

Cause 1 of 3

A while loop that reads an array with each()

1Check

This is the classic form. Nothing in the loop changes the array pointer outside the loop.

grep -rn "= each(" --include=*.php .

2Fix

Use foreach, which the PHP manual names as the replacement.

// old
while (list($key, $value) = each($array)) {
    // ...
}

// new
foreach ($array as $key => $value) {
    // ...
}

3Verify

Run the file and the tests that cover it.

php -l file.php && php file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

Code that needs control over the internal array pointer

1Check

If the loop stops and resumes, or is shared between functions, foreach may not reproduce it.

grep -rn "reset(\|next(\|prev(" --include=*.php .

2Fix

Rebuild the loop with key(), current() and next(), which the manual lists next to each().

reset($items);
while (($key = key($items)) !== null) {
    $value = current($items);
    // ...
    next($items);
}

3Verify

Compare the output with the old behaviour on a sample array.

php file.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A library or plugin that still calls each()

1Check

The PHP 8.0 migration guide lists each() as removed, so every package must be on a release made for PHP 8.

composer outdated

2Fix

Update the package to a release that supports PHP 8. If none exists, replace it, or patch your own copy using the foreach form above.

composer update vendor/package

3Verify

Load the page or script that failed before.

php -v && php script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run a static check on a PHP 8 version before you upgrade the server.
  • Keep dependencies on versions that list your PHP version as supported.
  • Write loops with foreach, and use pointer functions only when the logic needs them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.