The each() function is deprecated. This message will be suppressed on further calls PHP 7.2

The each() function is deprecated. This message will be suppressed on further calls PHP 7.2

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The each() Function Is Deprecated: Migrating from PHP 5 to PHP 7.2

When you upgrade a server from PHP 5 to PHP 7.2, encountering immediate deprecation warnings is often less about code breaking and more about adopting modern iteration patterns. The error The each() function is deprecated signals that your application relies on an internal mechanism that was fundamentally redesigned in the transition between major versions. Specifically, the line while(list($file, $info) = each($this->images)) triggers this warning because each() has been removed from the active language feature set.

The root cause lies in how PHP handles arrays and iteration efficiency. In PHP 5, each() returned the current element of an array while also advancing the internal pointer, returning a four-element array containing the key, value, and the previous/next keys. This made it convenient for old-school iteration but inefficient and memory-intensive because it required recreating the entire key-value pair structure on every call. More critically, each() modified the array pointer internally in ways that conflicted with modern expectations of how arrays should be traversed.

In PHP 7.2, this function was deprecated to force developers toward more robust alternatives. The warning message explicitly states it will be suppressed on further calls, but relying on a deprecated function is bad practice and can lead to unpredictable behavior if underlying implementation details change again in future versions. To resolve the error thrown by your web server after the restart, you must replace the each() call with a standard loop construct.

The most direct replacement for your specific code snippet is using foreach. The original line iterates over an associative array ($this->images), extracting keys and values into $file and $info. Here is the modern equivalent:

foreach ($this->images as $file => $info) {
    // Your logic here
}

This approach is superior for several reasons. First, it does not require manipulating the internal array pointer manually; foreach handles iteration cleanly and efficiently. Second, it avoids the performance penalty of reconstructing the full element structure on every step. Third, the syntax is clearer, making the intent of the code immediately obvious to other developers reading the file.

If you specifically need to preserve the key and value in separate variables without using foreach, you can use a standard while loop with reset() and next(), though this is rarely necessary for simple iteration tasks:

if (count($this->images)) {
    reset($this->images);
    while (($file => $info) = current($this->images)) {
        // Logic here
        next($this->images);
    }
}

However, this pattern is verbose and prone to errors if the array is empty or modified during iteration. Stick with foreach unless you have a very specific requirement that demands pointer manipulation.

Another common pitfall when upgrading legacy code is forgetting to handle cases where the array might be empty. The original each() function returned false immediately if called on an empty array, which could cause silent failures in logic expecting valid data. With foreach, the loop simply does not execute, allowing you to place validation logic outside the loop or inside using a flag if necessary.

If you are maintaining a large codebase from the PHP 5 era, scanning for each() usage is a high-priority task. While the warning is currently non-fatal and suppressed after the first call, relying on deprecated functions prevents you from taking full advantage of PHP 7's performance optimizations and security improvements. Updating these snippets now ensures your application remains compatible with long-term support schedules and prepares it for future versions where each() might be removed entirely without notice.

For developers working within the Laravel ecosystem specifically, this migration path is even more critical. While Laravel itself provides robust collection helpers that abstract away low-level array iteration, older custom scripts or legacy packages may still contain these patterns. Ensuring your base code is free of deprecated functions aligns with modern best practices for maintaining stable, high-performance web applications.

Our network