Nitesh Nair
Nitesh Nair

Reputation: 3

PHP Warning: Parameter must be an array or an object that implements Countable using xampp

I am trying to run a php program using xampp but it shows the following error:

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

The part of code where the error is being shown is given below:

    if (count($this->handles) >= $this->maxHandles) {
        curl_close($resource);
    } else {
        // Remove all callback functions as they can hold onto references
        // and are not cleaned up by curl_reset. Using curl_setopt_array
        // does not work for some reason, so removing each one
        // individually.
        curl_setopt($resource, CURLOPT_HEADERFUNCTION, null);
        curl_setopt($resource, CURLOPT_READFUNCTION, null);
        curl_setopt($resource, CURLOPT_WRITEFUNCTION, null);
        curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, null);
        curl_reset($resource);
        $this->handles[] = $resource;
    }

Upvotes: 0

Views: 237

Answers (2)

user206
user206

Reputation: 1105

the count() method in php >= 7.2, if it receives invalid values, null and false, it will send the following error:

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

This item in php8:

Fatal error: Uncaught TypeError: count(): Argument # 1 ($var) must be of type Countable...

As a result, before checking with the count() method, you can first check that the countable types are not invalid. (For example, according to your needs, use the empty() method before that or give an empty array value or ... ) or use methods such as is_array or is_object or ...

7.2.0 count() will now yield a warning on invalid countable types passed to the value parameter.

Upvotes: 0

Raymond
Raymond

Reputation: 78

First check if $this->handles is a array or object like this:

if(is_array($this->handles) || is_object($this->handles))
{
        if (count($this->handles) >= $this->maxHandles) {
        curl_close($resource);
    } else {
        // Remove all callback functions as they can hold onto references
        // and are not cleaned up by curl_reset. Using curl_setopt_array
        // does not work for some reason, so removing each one
        // individually.
        curl_setopt($resource, CURLOPT_HEADERFUNCTION, null);
        curl_setopt($resource, CURLOPT_READFUNCTION, null);
        curl_setopt($resource, CURLOPT_WRITEFUNCTION, null);
        curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, null);
        curl_reset($resource);
        $this->handles[] = $resource;
    }
}
else
{
    echo $this->handles." isn't a array or object";
}

Upvotes: 1

Related Questions