Tevin Mosley
Tevin Mosley

Reputation: 45

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/

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.

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?

Upvotes: 0

Views: 3290

Answers (1)

Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6668

In the general, solution is simple:

First debbug with var_dump() what $photos return. Than you will know what's the issue.

count() accept array and if you have false, null or anything else you will have error.

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(); before $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.


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 can be translated to 0 but in your case you can also get error. For that case I replace false with 0, add empty() and is_numeric() check and you have no errors there.

Upvotes: 1

Related Questions