Home » Web Design and Development » PHP » Solved : count(): Parameter must be an array or an object that implements Countable , PHP 7.3 error

Solved : count(): Parameter must be an array or an object that implements Countable , PHP 7.3 error

When we recently changed our website’s PHP version from 7.1 to 7.3 , we started getting following error for one of our website,

 ERROR: ErrorException [ 2 ]: count(): Parameter must be an array or an object that implements Countable

When we searched into internet, found this error is related to operations on array in PHP and similar error also pointed out that “Parameter must be an array” .. to try and understand further, when we looked at the code in our website where this error has been reported, it was something like below,

<?if(count($ads)): 
    … some code … 
<?elseif (count($ads) == 0):?>
    … some code … 
<?if?>

As we looked further, we found that “$ads” is an array and “count($ads)” is trying to get the size of array or number of elements in this array.

Solution : use empty check on array instead of “count”

But there was one catch, $ads , the array was resulting as empty when some of our webpages were getting hit, so when count is called on this empty array i.e. count($ads) it was throwing an error as “count(): Parameter must be an array or an object that implements Countable” which is because count was getting called on empty array.

Hence once we changed our code to use “empty” API instead of “count” everything started working.. so our above code became as,

<?if( !empty($ads)):
    … some code … 
<?elseif (empty($ads)):?>
    … some code … 
<?if?> 

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment