“Call to undefined function each()” after a PHP 8 update: find it and fix it

By CompatNav · Published · Last reviewed · 3 min read

Short answer

It’s a fatal error from PHP 8.0 and later: a plugin or theme calls each(), a function PHP removed in 8.0 after deprecating it in 7.2. The page stops and WordPress shows “There has been a critical error on this website”. Switch PHP back if the site is down, then update the plugin or theme named in the message, or send it to its developer. A check that reads code finds every call before you switch.

The message, exactly

An old menu plugin reads the first entry of a list with each(). On PHP 7.4 it works with a notice; on PHP 8.0 the page stops:

wp-content/plugins/menu-icons-old/menu.php

<?php
$items = ['home' => 'Home', 'shop' => 'Shop'];
reset($items);
$first = each($items);
echo $first['key'], ' => ', $first['value'], "\n";

Output on PHP 7.4.33

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /var/www/html/wp-content/plugins/menu-icons-old/menu.php on line 4
home => Home

Output on PHP 8.0.30the script stopped

Fatal error: Uncaught Error: Call to undefined function each() in /var/www/html/wp-content/plugins/menu-icons-old/menu.php:4
Stack trace:
#0 {main}
  thrown in /var/www/html/wp-content/plugins/menu-icons-old/menu.php on line 4

php.net’s page for the function: “This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0.” (php.net) And the PHP 8.0 migration guide: “each() has been removed. foreach or ArrayIterator should be used instead.” (php.net)

On a WordPress site you see “There has been a critical error on this website.” and the administrator gets an email that names the plugin or theme.

First, get the site back

  1. Switch PHP back to the version you had, in your hosting panel. The site works again at once; nothing is lost.
  2. Or pause only the culprit: the link in WordPress’s email opens the dashboard in recovery mode, where you can deactivate it.

Both are explained step by step in the critical error guide.

Find the plugin or theme

The path in the message names it: the folder after wp-content/plugins/ or wp-content/themes/, here menu-icons-old. Then:

  1. Update it (Dashboard → Updates).
  2. Already on the latest version? Send the full message to its developer.
  3. No update since PHP 8.0? Replace it. Code that still calls each() hasn’t been maintained for PHP 8 and probably has other problems too.

Found before you switch

A check that reads code finds every call while the site still runs PHP 7.4:

CompatNav skips calls that the code protects with function_exists( 'each' ): on PHP 8 the protected code simply doesn’t run, and nothing breaks.

PHP 8.0: the guarded call is skipped, the script finishes, and CompatNav reports nothing.

wp-content/plugins/menu-icons-old/menu.php

<?php
if (function_exists('each')) {
    $items = ['home' => 'Home'];
    $first = each($items);
}
echo 'Done', "\n";

Output on PHP 8.0.30

Done

each() usually doesn’t come alone: old code that uses it often also has create_function() or curly-brace offsets, removed in the same PHP version. The full PHP 8.0 list is in moving from PHP 7.4 to PHP 8.

Key takeaways

  • each() was deprecated in PHP 7.2 and removed in PHP 8.0. On PHP 8 every call is a fatal error.
  • On PHP 7.4 the same code only showed a deprecation notice, once.
  • The path in the message names the plugin or theme: the folder after wp-content/plugins/ or wp-content/themes/.
  • The usual replacement is a foreach loop; for the first entry, array_key_first().
  • A check that reads code finds every call before you upgrade, and skips calls guarded by function_exists().

Frequently asked questions

Why did it work before the PHP update?

PHP 7.2 to 7.4 still had each() and only showed a deprecation notice, which most sites never displayed. PHP 8.0 removed the function, so the call stops the page.

Can I add the function back?

A plugin can define its own each() for old code, but that hides the problem: the plugin still needs updating. Update it, or replace it if it’s no longer maintained.

Is create_function() the same problem?

Yes, the same story: deprecated in PHP 7.2, removed in 8.0, a fatal error on PHP 8. It has its own guide, linked on this page.

Check your own site before you upgrade

CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.

Get CompatNav on wordpress.org How it works

Sources

About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.