count(): Parameter must be an array or an object that implements Countable (View: )

1、报错:count(): Parameter must be an array or an object that implements Countable (View: )。如图1

图1

2、查看代码实现

count($user->address_book);

3、打印 $user->address_book,其值为 NULL

4、调整代码实现,当 $user->address_book 为 null,表达式 $user->address_book ?? [] 等同于 [],否则为 $user->address_book。

count($user->address_book ?? []);

5、在 PHP 7.2.0 时,当 value 参数传入了无效的 countable 类型, count() 现在会产生警告。发生错误的环境的版本是 PHP 7.4.26

永夜