Common PHP Warnings/ Errors and their Solutions

Recently, we have updated our PHP version to the latest stable version (7.4.9) for our WordPress website. After the update we started getting PHP warnings. We have listed some of the warnings with their possible solutions in this blog post.

E_WARNING:

count(): Parameter must be an array or an object that implements Countable

Solution:

// replace this kind of code:
if (count($array_name) > 0)
// with
if (is_countable($array_name) && count($array_name) > 0)

E_WARNING:

Invalid argument supplied for foreach()

Solution:

// replace following kid of code:
foreach ($array_name as $item) {
 // ...
}
// with
foreach ((array) $array_name as $item) {
 // ...
}

References:

  • https://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach
  • https://wordpress.org/support/topic/count-parameter-must-be-an-array-or-an-object-that-implements-countable-6/

Leave a Comment

Your email address will not be published. Required fields are marked *