繁体   English   中英

count():参数必须是在其中实现Countable的数组或对象

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

我在我的网站上收到此警告,我只是获得有关IP是否​​可用的信息,它正在工作,它说它不可用,但它也显示有关代码的警告,我进行了搜索,但这些答案都没有帮帮我。 因此代码正在运行,只想摆脱此警告。 PHP 7.x不支持这样的语法? 问题出在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;
    }

get_headers()失败时,它返回falsecount(false) 从PHP / 7.2demo开始触发该警告。 这样做的理由是,计算布尔假并得到1并没有多大意义。

也许您只是想要这样:

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

如果$ip存在, get_headers($ ip)为您提供一个数组,否则返回FALSE。 因此,无论是否为数组,都应检查get_headers()的返回值。 一个例子:

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

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

注意 :PHP count需要第一个参数为数组或可计数对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM