2

I just upgraded my PHP Version from 5.6 to 7.4. I used count() function in my page, example:

$watch_server_count  = count($watch_server);
if ($watch_server_count > 0) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {

    }
}

Warning: count(): Parameter must be an array or an object that implements Countable in...

Rain
  • 3,416
  • 3
  • 24
  • 40
user4158643
  • 21
  • 1
  • 5
  • 1
    Check what `$watch_server` contains. – Nigel Ren May 12 '20 at 06:52
  • 3
    `$watch_server` is an object, not an array, hence the `$watch_server->...`. Did you mean `$watch_server_count = count($watch_server->table);` ? This would make sense since you are iterating this property – Cid May 12 '20 at 06:54
  • object(ShinoDB)#6137 (3) { ["error"]=> NULL ["filename"]=> string(187) "OK^https://ok.ru/videoembed/963440478908 Vidbom^https://www.vidbm.com/embed-qx0qzffbptht.html Vevio^https://vev.io/embed/k4rdekk1q2o1/ Vidshare^https://vidshare.tv/embed-l8w79d88qq1v.html" ["table"]=> array(4) { [0]=> array(2) { [0]=> string(2) "OK" [1]=> string(37) "https://ok.ru/videoembed/963440478908" } } } – user4158643 May 12 '20 at 06:57
  • Does this answer your question? [count(): Parameter must be an array or an object that implements Countable error in php](https://stackoverflow.com/questions/61542767/count-parameter-must-be-an-array-or-an-object-that-implements-countable-error) – compuphys May 12 '20 at 08:30
  • 2
    Does this answer your question? [PHP count replacement](https://stackoverflow.com/questions/49855519/php-count-replacement) – Rain May 12 '20 at 09:35
  • In PHP 7.2 and above versions count() function just accepting array as an argument. Check this topic [Upgrade to PHP 7.2 caused error in function counting form input array ](https://laracasts.com/discuss/channels/laravel/upgrade-to-php-72-caused-error-in-function-counting-form-input-array) – Amir Etemad May 20 '20 at 17:45
  • Please add all information to your answer instead of linking to external ressources – Nico Haase Aug 12 '20 at 14:39

2 Answers2

1

You can try this way. is_countable

https://www.php.net/manual/en/function.is-countable.php

if ( is_countable($watch_server->table) ) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {
        ...
    }
}
  • 1
    Hi Mehmet, welcome to Stackoverflow. If you post an answer, please check whether there are already other answers covering the same code. In this case, there's another answer from Redy S. Additionally, your answer might be wrong, as not all objects that implement the `Countable` interface provide the neccessary methods to iterate – Nico Haase Aug 12 '20 at 14:38
0

Since PHP 7.1, you can use is_iterable before performing foreach.

(PHP 7 >= 7.1.0) is_iterable — Verify that the contents of a variable is an iterable value

https://www.php.net/manual/en/function.is-iterable.php

So the code will look like this:

if ( is_iterable($watch_server->table) ) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {
        //
    }
}
Redy S
  • 362
  • 1
  • 6