简体   繁体   English

如何修复 PHP 7.2 警告:count():参数必须是一个数组或实现 Countable in errors.php 的数组或 object?

[英]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 官方文档:

PHP count() PHP 计数()

PHP is_countable() PHP is_countable()

PHP is_array() PHP is_array()

暂无
暂无

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

相关问题 PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable PHP警告:count():参数必须是数组或实现Countable的对象? - PHP Warning: count(): Parameter must be an array or an object that implements Countable? PHP 错误 - 警告:count():参数必须是实现 Countable 的数组或对象 - PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable phpdoc:PHP警告:count():参数必须是实现Countable的数组或对象 - phpdoc: PHP Warning: count(): Parameter must be an array or an object that implements Countable PHP错误:警告:count():参数必须是实现Countable的数组或对象 - PHP error: Warning: count(): Parameter must be an array or an object that implements Countable PHP 7.3 警告:count():参数必须是实现 Countable 的数组或对象 - PHP 7.3 Warning: count(): Parameter must be an array or an object that implements Countable PHP 7.3 警告 count():参数必须是数组或实现 Countable 的 object - PHP 7.3 Warning count(): Parameter must be an array or an object that implements Countable PHP: 警告: count(): 参数必须是数组或实现 Countable 的对象 - PHP: Warning: count(): Parameter must be an array or an object that implements Countable 警告:sizeof():参数必须是一个数组或实现 Countable php7.2 的 object - Warning: sizeof(): Parameter must be an array or an object that implements Countable php7.2 yii2 gridview count():参数必须是实现Countable php 7.2的数组或对象 - yii2 gridview count(): Parameter must be an array or an object that implements Countable php 7.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM