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 appeared at 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

De ce apare eroarea 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 subsequent versions, can generate this error, if the given parameter is not a array or an object that implements the interface Countable.

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

How do you solve PHP errors generated by sizeof()?

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

In the case of those who use the old versions of the module Cross Sell Product Display, the solution is simple. The functions from line 18 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 SIZEOF () will be replaced with:

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 change first checks if $crosssells It's a array using the function is_array() and otherwise returns false.

If $crosssells It's a array, the function is used count() to determine the number of items in array. If the number of items is zero or $crosssells It is a vacuum string, it returns false.

Leave comments if clarifications or completions are to be made to this tutorial.

Passionate about technology, I write with pleasure on stealthsetts.com starting with 2006. I have a rich experience in operating systems: Macos, Windows and Linux, but also in programming languages ​​and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, Presashop).

Home » Your source of IT tutorials, useful tips and news. » Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable
Leave a Comment