Alves
Alves

Reputation: 23

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

I'm getting this warning on my website, im just getting the information is it IP available or not, it's work, its says it's unavailable but it shows an warning also about code, I searched about it, but none of those answers didn't help me. So the code is working, just want to get off this warning. Php 7.x doesn't support syntax like this? The problem is in if (count($test)==1).

$printers = file("printers.txt"); //input txt file with IP addresses in chosen order
    $number_of_printers = count ($printers);
    for ( $i = 0 ; $i < $number_of_printers ; $i++)
    {
    $ip=str_replace("\r\n","",$printers[$i]);
    $ip=str_replace("\r","",$ip);
    $ip=str_replace("\n","",$ip);
    $test=@get_headers("http://$ip");
    if (count($test)==1){

        //do stuff here
        echo "<div class='printerwrapper'<div class='location'>$ip is unavailable</div></div>";
        continue;
    }

Upvotes: 0

Views: 1830

Answers (2)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146660

When get_headers() fails it returns false and count(false) triggers that warning since PHP/7.2 (demo). The rationale is that counting a boolean false and getting 1 doesn't make much sense.

Perhaps you just want this:

if (!$test) {
    //do stuff here
    echo "<div class='printerwrapper'<div class='location'>$ip is unavailable</div></div>";
    continue;
}

Upvotes: 3

MH2K9
MH2K9

Reputation: 12039

get_headers($ip) provides you an array if the $ip is exist, otherwise it returns FALSE. So you should check the return value of get_headers() weather it is an array or not. An example:

$headerInfo = @get_headers("http://127.0.0.1");

if (is_array($headerInfo) && count($headerInfo)) {
    // do something
} else {
    // do others
}

Note: PHP count demands the first argument an array or countable object.

Upvotes: 0

Related Questions