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,

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

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 … 
Add or remove fields in Salesforce ... x
Add or remove fields in Salesforce Omni Channel Widgets

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system 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, Debugging and Performance Tools Development and Build 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 NDK / Middleware / HAL Operating System 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 Embedded Linux