You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP 7.4中“尝试访问bool类型值的数组偏移量”报错的修复方案

PHP 7.4下「Trying to access array offset on value of type bool」报错修复方案

这个报错的核心原因是array_search($channel, $this->pages)在未找到匹配项时会返回false,而PHP 7.4开始严格校验数组偏移量的合法性,直接将false作为数组下标访问就会触发该Notice提示。

修复时需要先校验array_search的返回值是否有效,具体修改如下:

if ($info['msg'] == CURLMSG_DONE) {
    $channel = $info['handle'];
    $page = array_search($channel, $this->pages);
    // 先确认找到有效下标再操作数组
    if ($page !== false) {
        $this->pages[$page] = curl_multi_getcontent($channel);
        $this->multiHandlingRemove($channel);
        curl_close($channel);
    }
}

需要注意的是,判断时必须用!== false而不是!$page,因为如果$channel在$this->pages的第0位,array_search会返回0,此时!$page会误判为未找到。

内容的提问来源于stack exchange,提问作者Дмитрий Мышков

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 04:05:15