[英]How to fix PHP 7.2 Warning: count(): Parameter must be an array or an object that implements Countable in errors.php?
My script is-我的剧本是——
<?php if (count($errors) > 0) : ?>
<div class="error">
<?php foreach ($errors as $error) : ?>
<p><?php echo $error ?></p>
<?php endforeach ?>
</div>
<?php endif ?>
The error I get is Warning: count(): Parameter must be an array or an object that implements Countable
.我得到的错误是
Warning: count(): Parameter must be an array or an object that implements Countable
。
I'm using PHP 7.2
on Apache2.我在 Apache2 上使用
PHP 7.2
。
As the error says, Parameter must be an array or an object that implements Countable
, the $errors
should be an array.正如错误所说,
Parameter must be an array or an object that implements Countable
$errors
应该是一个数组。
In your case, it might be empty.在您的情况下,它可能是空的。 So before using
count()
you should always check it implements a Countable interface.因此,在使用
count()
之前,您应该始终检查它是否实现了 Countable 接口。
I assume you are trying to iterate an Array
and for that I first check the $errors
is an array using is_array()
.我假设您正在尝试迭代一个
Array
,为此我首先使用is_array()
检查$errors
是一个数组。
<?php if (is_array($errors) && count($errors) ) : ?>
<div class="error">
<?php foreach ($errors as $error) : ?>
<p><?php echo $error ?></p>
<?php endforeach ?>
</div>
<?php endif ?>
You may also need to look at is_countable()
, link below:您可能还需要查看
is_countable()
,链接如下:
PHP Official Documentation: PHP 官方文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.