尝试在PHP 7.4中访问值的数组偏移量

wyyhbhjk  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(140)

我刚刚升级我的服务器的PHP版本到PHP 7.4.1,现在得到这个错误:注意:尝试访问中bool类型值的数组偏移量

if ($info['msg'] == CURLMSG_DONE) {
    $channel = $info['handle'];
    $page = array_search($channel, $this->pages);
    $this->pages[$page] = curl_multi_getcontent($channel);
    $this->multiHandlingRemove($channel);
    curl_close($channel);
}

PHP 7.4的修复程序是什么?

pdkcd3nj

pdkcd3nj1#

出现错误的原因是array_search将在未找到任何内容时返回false,并且布尔值不能用作数组偏移量(也称为数组索引)。
这表明您的脚本在某些场景中可能已经产生了不希望的结果,您需要检查是否没有返回false:

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);
}

这里有一个稍微改进的版本,为了咯咯地笑:

if ($info['msg'] == CURLMSG_DONE) {
    $channel = $info['handle'];

    if (isset($this->pages[$channel])) {
        $this->pages[$channel] = curl_multi_getcontent($channel);
        $this->multiHandlingRemove($channel);
    }

    curl_close($channel);
}

在这里,它是一行程序,因为我喜欢它(不要使用它,它在任何意义上都不优雅):

if($info['msg'] == CURLMSG_DONE) isset($this->pages[$channel = $info['handle']]) && ($this->pages[$channel] = curl_multi_getcontent($channel) && $this->multiHandlingRemove($channel)) && curl_close($channel);

相关问题