php - 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;
}
解决方案
当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;
}
推荐阅读
- mongodb - 如何修复 Mac OS 11 上的“mongod”错误?
- python - 将最后一个卷积层的输出传递到 FCC 层 - PyTorch
- php - 如何让 wkhtmtopdf 在没有 root 访问权限的情况下在共享主机上工作
- javascript - 如果从用户浏览器发出请求,CORS 将如何响应
- bash - Tee 命令未附加
- r - 停止在 R 中重新排序 Y 轴的 Geom-Point
- node.js - Cron 运行 2 次 node-cron
- javascript - 从localStorage加载值后如何设置样式?
- javascript - 淡入 javascript 中的背景以及视频
- reactjs - 如何使用 Typescript 重构 React 中的道具?