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...

7

2 Answers 2

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) {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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) {
        //
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.