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

많이 나타나는 PHP 오류 WordPress Plugins 오랫동안 업데이트되지 않았거나 최신 버전의 PHP와 호환되지 않습니다. PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable.

이 시나리오에서는 모듈에서 PHP 오류가 발생했습니다. Cross Sell Product Display 용 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

오류가 발생하는 이유 PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable ?

이 PHP 오류를 생성하는 문제는 함수입니다. sizeof() PHP 7.2 이상 버전에서 주어진 매개변수가 하나가 아닌 경우 이 오류를 생성할 수 있습니다. array 또는 인터페이스를 구현하는 객체 Countable.

따라서 PHP 버전 업데이트 후 오류가 자주 발생합니다.

에 의해 생성된 PHP 오류를 해결하는 방법 sizeof()?

가장 간단한 방법은 함수 호출을 대체하는 것입니다. sizeof() 함수 호출로 count().

이전 버전의 모듈을 사용하시는 분들의 경우 Cross Sell Product Display, 해결책은 간단합니다. 18인치 라인의 기능이 대체됩니다. 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;
	
}

그것이있는 위의 코드 sizeof() 다음으로 대체됩니다.

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;
	
}

이 수정은 먼저 다음을 확인합니다. $crosssells 하는 array 기능을 사용하여 is_array() 그렇지 않으면 반환 false.

의 경우 $crosssells 하는 array, 기능이 사용됩니다 count() 요소의 수를 결정하기 위해 array. 요소 수가 XNUMX이거나 $crosssells 빈 문자열이면 false가 반환됩니다.

이 튜토리얼에 대한 설명이나 추가 사항이 있으면 의견을 남겨주세요.

기술에 대한 열정, 나는 기쁨으로 글을 씁니다. StealthSettings2006년부터 .com을 운영하고 있습니다. 저는 운영 체제 분야에서 폭넓은 경험을 갖고 있습니다. macOS, Windows 과 Linux, 프로그래밍 언어 및 블로그 플랫폼(WordPress) 및 온라인 상점의 경우(WooCommerce, 마젠토, 프레스타샵).

방법 » WordPress » 수정 PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable 
코멘트 남김