php7.2没问题但php7.4报错:Trying to access array offset on value of type null

php7.2没问题但php7.4报错:Trying to access array offset on value of type null

岳小威子
2022-08-31 / 85 个字 / 0 评论 / 101 阅读 / 加载耗时:18ms / 正在检测是否收录...
温馨提示:
本文最后更新于2022年08月31日,已超过575天没有更新,若内容或图片失效,请留言反馈。

一、说明

报错原因:访问类型为 null 的值的 数组下标
7.2中是没问题的,7.4中对语法规范性更为严谨

二、原因

原文:Trying to use values of type null, bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.
翻译:尝试将 null,bool,int,float 或 resource 类型的值用作数组 ( 例如 $null["key"] ) 会产生一个通知。
传送门: https://www.php.net/manual/en/migration74.incompatible.php ,截图如下:
2907827071.webp

三、解决

# php7.4错误代码
$b = NULL;
$a = $b['key'] ? $b['key'] : 0;

# 修改后正确代码
$b = NULL;
$a = isset($b['key']) && !empty($b['key']) ? $b : 0;

文章到这里就结束了

您在本文章已经停留了大概
喜欢的话就点个赞吧!或着请我喝个冰可乐,我就太感谢你了!


0
打赏
拜谢打赏(☆ω☆)

感谢老板,老板大气。。

评论 (0)

取消