首页 > 解决方案 > count():参数必须是数组或者实现了Countable的对象

问题描述

我在我的网站上收到此警告,我只是获取信息是否 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;
    }

标签: php

解决方案


get_headers()失败时,它会返回false并触发PHP/7.2演示)以来count(false)的警告。基本原理是计算布尔错误并获取没有多大意义。1

也许你只是想要这个:

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

推荐阅读