简体   繁体   English

PHP警告:count():参数必须是数组或实现Countable的对象?

[英]PHP Warning: count(): Parameter must be an array or an object that implements Countable?

On my website I allow people to upload image galleries. 在我的网站上,我允许人们上传图片库。 When they click an image there is a next and previous button at the bottom so they can easily scroll back and forth through the images. 当他们点击图像时,底部有一个下一个上一个按钮,因此他们可以轻松地在图像中来回滚动。

I am getting the below error in my log located at /opt/cpanel/ea-php72/root/usr/var/log/php-fpm/ 我的日志位于/ opt / cpanel / ea-php72 / root / usr / var / log / php-fpm /我收到以下错误

NOTICE: PHP message: PHP Warning:  count(): Parameter must be an array or an object that implements Countable in . . . on line 12

It is talking about the line below in my code: 它在我的代码中讨论下面的行:

$max = count($photos);

Below is the other code that comes with that line: 以下是该行附带的其他代码:

$photos = get_field('gallery');
$max = count($photos);    <------- error line here -------->
$current = (isset($_GET['image'])) ? intval($_GET['image']) : false;
if ($current !== false) {
    if ($current > $max) $current = $max;
    if ($current < 1) $current = 1;
}

$next = (($current + 1) < $max) ? ($current + 1) : $max;
$prev = (($current - 1) > 1) ? ($current - 1) : 1;
?>

Basically this code uses get_field('gallery') to get the total amount of photos in the gallery and the number is assigned to variable max . 基本上这段代码使用get_field('gallery')来获取图库中的照片总数,并将数字分配给变量max

The rest of the code is how the next and previous buttons work. 其余代码是下一个上一个按钮的工作方式。

I do not know what is wrong. 我不知道出了什么问题。 Can somebody help with this? 有人可以帮忙吗?

In the general, solution is simple: 总的来说,解决方案很简单:

First debbug with var_dump() what $photos return. var_dump()第一个debbug是什么$photos返回。 Than you will know what's the issue. 比你知道什么是问题。

count() accept array and if you have false , null or anything else you will have error. count()接受数组,如果你有falsenull或其他任何你将有错误。

Just do something like this: 做这样的事情:

$photos = get_field('gallery');
if(!is_array($photos) || empty($photos)) $photos = array(); // FIX ERROR!!!
$max = count($photos);    <------- error line here -------->
$current = (isset($_GET['image']) && !empty($_GET['image']) && is_numeric($_GET['image'])) ? intval($_GET['image']) : 0;
if ($current > 0) {
    if ($current > $max) $current = $max;
    if ($current < 1) $current = 1;
}

$next = (($current + 1) < $max) ? ($current + 1) : $max;
$prev = (($current - 1) > 1) ? ($current - 1) : 1;
?>

With if(!is_array($photos) || empty($photos)) $photos = array(); 使用if(!is_array($photos) || empty($photos)) $photos = array(); before $max = count($photos); $max = count($photos); you fixing your issue and anything what is not array or empty (0, NULL, FALSE, '') will be fixed and you will have in the count $max result 0 because array is empty. 你修复你的问题,任何不是数组或空的东西(0,NULL,FALSE,'')将被修复,你将在count $max结果0因为数组为空。


IMPORTANT!!! 重要!!!

You should not work like this. 你不应该这样工作。 You need to know what information you receive and expect in variables. 您需要知道您在变量中收到和期望的信息。 The code must remain clean and correcting such errors is a bad practice. 代码必须保持干净并纠正这些错误是一种不好的做法。 If you receive an array, then the array is expected and you must have checks before any calculating. 如果您收到一个数组,那么该数组是预期的,您必须在进行任何计算之前进行检查。


UPDATE: 更新:

Also you have error in 你也有错误

$current = (isset($_GET['image'])) ? intval($_GET['image']) : false;
if ($current !== false)

I fix it to work like this: 我修复它像这样工作:

$current = (isset($_GET['image']) && !empty($_GET['image']) && is_numeric($_GET['image'])) ? intval($_GET['image']) : 0;
if ($current > 0)

Reason for that is that you have calculations below and you not expect (false + 1) to be something good. 原因是你有下面的计算,你不希望(false + 1)是好事。 false can be translated to 0 but in your case you can also get error. false可以转换为0但在您的情况下,您也可能会出错。 For that case I replace false with 0 , add empty() and is_numeric() check and you have no errors there. 对于这种情况,我将false替换为0 ,添加empty()is_numeric()检查,那里没有错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 错误 - 警告:count():参数必须是实现 Countable 的数组或对象 - PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable phpdoc:PHP警告:count():参数必须是实现Countable的数组或对象 - phpdoc: PHP Warning: count(): Parameter must be an array or an object that implements Countable PHP错误:警告:count():参数必须是实现Countable的数组或对象 - PHP error: Warning: count(): Parameter must be an array or an object that implements Countable PHP 7.3 警告:count():参数必须是实现 Countable 的数组或对象 - PHP 7.3 Warning: count(): Parameter must be an array or an object that implements Countable PHP 7.3 警告 count():参数必须是数组或实现 Countable 的 object - PHP 7.3 Warning count(): Parameter must be an array or an object that implements Countable PHP: 警告: count(): 参数必须是数组或实现 Countable 的对象 - PHP: Warning: count(): Parameter must be an array or an object that implements Countable 警告:count():参数必须是一个数组或一个实现 Countable 的对象 / if( count($query) ) { - Warning: count(): Parameter must be an array or an object that implements Countable / if( count($query) ) { Wordpress 错误:警告:count():参数必须是实现 Countable 的数组或对象 - Wordpress error: Warning: count(): Parameter must be an array or an object that implements Countable Error = Warning: count(): Parameter must be an array or an object that implement Countable in - Error = Warning: count(): Parameter must be an array or an object that implements Countable in 警告:count():必须是实现Countable的数组或对象 - Warning: count(): must be an array or object that implements Countable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM