PHP error guide
Call to undefined function each: meaning and fix
Error summary
Replace pointer-driven each loops with foreach. PHP 8 removed each() after its PHP 7.2 deprecation.
What it means
Replace pointer-driven each loops with foreach. PHP 8 removed each() after its PHP 7.2 deprecation.
What the error means
This message means that PHP 8 removed each() after its PHP 7.2 deprecation. The exact signature distinguishes call to undefined function each from a generic application failure.
Why PHP produces it
The engine or service reports “Call to undefined function each” because its required precondition was not met. Replace pointer-driven each loops with foreach.
PHP version notes
The call to undefined function each wording here is based on PHP 8.0+; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: PHP 8 removed each() after its PHP 7.2 deprecation
- The code path assumes the prerequisite for call to undefined function each has already been satisfied.
- For call to undefined function each, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting call to undefined function each.
Minimal examples
BAD — reproduces the problem
while (list($k,$v)=each($items)) {}
FIXED — safer pattern
foreach ($items as $k => $v) {}
Step-by-step diagnosis
- Copy the complete “Fatal error: Uncaught Error: Call to undefined function each()” text and retain the first application stack frame.
- Reproduce call to undefined function each in the same SAPI and environment listed for this page.
- Before changing call to undefined function each, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—replace pointer-driven each loops with foreach—then repeat the original request once.
Fixes
Correct the failing prerequisite
Replace pointer-driven each loops with foreach
foreach ($items as $k => $v) {}
Fail explicitly at the boundary
Validate the condition before the operation that emits call to undefined function each, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing call to undefined function each instead of correcting its upstream condition.
- Testing call to undefined function each only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for call to undefined function each before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for call to undefined function each.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting call to undefined function each reproducible.
- Validate external data and service return values before they can trigger call to undefined function each.
Modernization guides
Web server / environment notes
cli, fpm, composer. The failure occurs where pHP 8 removed each() after its PHP 7.2 deprecation
Tags: cli,fpm,composer