Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable 

A PHP error that appears in many WordPress Plugins which have not been updated for a long time or are incompatible with newer versions of PHP. PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable.

In our scenario, the PHP error occurred in a module Cross Sell Product Display for WooCommerce.

FastCGI sent in stderr: "PHP message: PHP Warning:  sizeof(): Parameter must be an array or an object that implements Countable in /web/path/public_html/wp-content/plugins/cross-sell-product-display-for-woocommerce/templates.php on line 18

Why the error occurs PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable ?

The problem that generates this PHP error is the function sizeof() which in the version of PHP 7.2 or later versions, can generate this error, if the given parameter is not one array or an object that implements the interface Countable.

Therefore, the error often appears after an update of the PHP version.

How to solve PHP errors generated by sizeof()?

The simplest method is to replace the function call sizeof() with a function call count().

In the case of those who use old versions of the module Cross Sell Product Display, the solution is simple. The functions from the 18 in. line will be replaced templates.php.

function cdxfreewoocross_get_cross_sell_products($product_id=false){
	
	if($product_id ===false ){
		
		if(!is_product()){return false;}
		
		$product_id = (int)get_the_ID();
		if($product_id=='0'){return false;}
		
	}
	
	$crosssells = get_post_meta( $product_id, '_crosssell_ids',true);
	if ( sizeof($crosssells ) == 0  || $crosssells =='') { return false; }
	
	return $crosssells;
	
}

The above code in which it is sizeof() will be replaced by:

function cdxfreewoocross_get_cross_sell_products($product_id=false){
	
	if($product_id ===false ){
		
		if(!is_product()){return false;}
		
		$product_id = (int)get_the_ID();
		if($product_id=='0'){return false;}
		
	}
	
	$crosssells = get_post_meta( $product_id, '_crosssell_ids',true);
	if ( !is_array( $crosssells ) || count( $crosssells ) == 0  || $crosssells =='') { return false; }
	
	return $crosssells;
	
}

This modification first checks if $crosssells is a array using the function is_array() and, otherwise, returns false.

In case of $crosssells is a array, the function is used count() to determine the number of elements in array. If the number of elements is zero or $crosssells is an empty string, false is returned.

Leave comments if there are any clarifications or additions to this tutorial.

Passionate about technology, I enjoy writing on StealthSettings.com since 2006. I have a rich experience in operating systems: macOS, Windows, and Linux, as well as in programming languages and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, PrestaShop).

How to » WordPress » Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable 
Leave a Comment