Upgrade PHP 7.1.x to 7.2.x

on in Blog
Last modified on

When you are upgrading a WordPress plugin or theme to be compatible with PHP 7.2.x, here is a pattern on how to update while = each() loops to foreach() loops.

Case 1: Missing $value

reset($array);
while (list($key, ) = each($array)) {

Update to:

foreach(array_keys($array) as $key) {

Case 2: Missing $key

reset($array);
while (list(, $value) = each($array)) {

Update to:

foreach($array as $value) {

Case 3: Not missing anything

reset($array);
while (list($key, $value) = each($array)) {

Update to:

foreach($array as $key => $value) {

Related posts