找回密码
 新建账号

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

[复制链接]
php 发表于 2022/12/27 17:26 | 显示全部楼层 |阅读模式
Discuz! System Error
count(): Argument #1 ($value) must be of type Countable|array, null given
PHP Debug
No.FileLineCode
1forum.php71require()
2source/module/forum/forum_viewthread.php546break()

www.51-n.com 已经将此出错信息详细记录, 由此给您带来的访问不便我们深感歉意

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 行
  1. $realpost = count($postarr);
复制代码
由于代码不严谨,此时 $postarr 这个变量并没有定义,导致 null 被传递给了 count(),出现了 count(): Argument #1 ($value) must be of type Countable|array, null given 这个致命错误。

解决办法是将以上代码换成如下代码,如果 $postarr 没有定义就给它一个初始值。
  1. $postarr = isSet($postarr) ? $postarr : [];
  2. $realpost = count($postarr);
复制代码

手机版|轻松E站

GMT+8, 2024/4/27 04:55

快速回复 返回顶部 返回列表