Discuz 3.5 PHP 8 count(): Argument #1 ($value) must be of type Countable|array

本文阅读 1 分钟
首页 技术教程 正文

Discuz! System Error
count(): Argument #1 ($value) must be of type Countable|array, null given

PHP 8.0+ 给 count() 传递 null 为参数会触发致命错误 Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given,早在 PHP 7.2+ 就会触发 Warning 级别的错误 Warning: count(): Parameter must be an array or an object that implements Countable.

具体到 Discuz X 3.5 来说,Discuz! 报错从最后一个倒过来排查,在 source/module/forum/forum_viewthread.php 的第 546 行

$realpost = count($postarr);

也可能是:

$realpost = is_array($postarr) ? count($postarr) : 0;

由于代码不严谨,此时 $postarr 这个变量并没有定义,导致 null 被传递给了 count(),出现了 count(): Argument #1 ($value) must be of type Countable|array, null given 这个致命错误。

解决办法是将以上代码换成如下代码,如果 $postarr 没有定义就给它一个初始值。

 $postarr = isSet($postarr) ? $postarr : [];
$realpost = count($postarr);

找不到就是:

$postarr = isSet($postarr) ? $postarr : [];
$realpost = is_array($postarr) ? count($postarr) : 0;
本文来自投稿,不代表本站立场,如若转载,请注明出处:http://www.cnmini.net/index.php/archives/563/
常用脚本合集(持续更新中)
« 上一篇 08-30
按 F11 给 Win10/11资源管理器加速400%
下一篇 » 09-12

发表评论

成为第一个评论的人