Replacing each() in code that stopped working on PHP 8
Fatal 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.
Fix it fast
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 foreach2Match what you see, then jump to its fix
- 1
while (list($key, $value) = each($array))A while loop that reads an array with each()Fix - 2
reset(), next() or prev() are called around each()Code that needs control over the internal array pointerFix - 3
The fatal error points at a file under vendor/ or a plugins folderA library or plugin that still calls each()Fix
- 1
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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 outdated2Fix
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/package3Verify
Load the page or script that failed before.
php -v && php script.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix