[英]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()
接受数组,如果你有false
, null
或其他任何你将有错误。
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
因为数组为空。
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.
如果您收到一个数组,那么该数组是预期的,您必须在进行任何计算之前进行检查。
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.