报错为in_array(): Argument #2 ($haystack) must be of type array, null given
这个错误表示你在使用 in_array() 函数时,第二个参数($haystack)传入了 null,而不是数组。in_array() 函数的第二个参数必须是数组类型。PHP 8.0 中的 in_array() 错误处理PHP 8.0 对类型系统进行了更严格的检查,这就是为什么会出现这个错误。在 PHP 7.x 中,in_array() 的第二个参数传入 null 会返回 false 并发出警告,但在 PHP 8.0+ 中,这会引发致命错误。

第一步:定位错误位置
1.1 查看错误日志
在 Typecho 根目录的 config.inc.php 中开启调试模式:

php

/** 开启调试模式 */
define('__TYPECHO_DEBUG__', true);

刷新页面,你会看到类似这样的错误信息:

text

TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in 
/usr/themes/your-theme/sidebar.php on line 45

1.2 找到具体文件
根据错误信息,找到对应的文件:

如果错误在主题中:/usr/themes/your-theme-name/ 目录下

如果错误在插件中:/usr/plugins/plugin-name/ 目录下

如果错误在核心中:/var/Typecho/ 目录下
错误信息为

in_array(): Argument #2 ($haystack) must be of type array, null given
TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in /app/usr/themes/handsome/component/aside.php:168

使用编辑器打开文件:

bash
vim /app/usr/themes/handsome/component/aside.php
找到第168行(按 168G 直接跳到第168行)

将代码改为:

<?php if (!in_array('component', $this->options->asideSetting ?? [])): ?>

保存并退出(在 vim 中:按 ESC,然后输入 :wq,回车)
解释为什么这样修复
原代码:<?php if (@!in_array('component',$this->options->asideSetting)): ?>
问题:
$this->options->asideSetting 可能是 null
PHP 8.0 之前,@ 可以抑制警告
PHP 8.0 之后,类型错误变成异常,@ 无法抑制
修复原理:
$this->options->asideSetting ?? []:如果 asideSetting 为 null,使用空数组 [] 替代
这样就确保了 in_array() 的第二个参数永远是数组
移除了 @,因为不再需要错误抑制

总结
按照以下步骤操作:
备份原文件
使用方案2修复第168行代码
验证语法和功能
检查主题中是否有其他类似问题
修复后的代码应该能完全兼容 PHP 8.0+,并且逻辑与原代码保持一致。

Last modification:December 19, 2025
If you think my article is useful to you, please feel free to appreciate