Upgrade PHP 7.1.x to 7.2.x

👋 Ciprian on Thursday, February 15, 2018 in Blog
Last modified on Thursday, February 15, 2018

Learn JavaScript by example. Code snippets, how-to's and tutorials. Try now!

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) {

👋 Added by Ciprian on Thursday, February 15, 2018 in Blog. Last modified on Thursday, February 15, 2018.

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Policy