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.


Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


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.

Related :   Adding a Twitter Button and using Twitter Card for tweets

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?> 
lynxbee_ezoic

Leave a Comment

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded