如果我在 PHP 中有一个当前为空的数组,访问 undefined index 是否应该出现 E_NOTICE 级别的错误?
如果我有以下代码片段:
$myArray = null;
echo $myArray['foo']['bar'];
我预计会出现错误,但它运行没有问题。我已验证我的日志级别设置为 E_ALL。只要您不尝试修改数据,我是否遗漏了什么或者 PHP 是否乐意为 undefined index 返回 null?
最佳答案
是的, undefined index 只触发非空变量(不要问我为什么)。不过这会触发通知:
<?php
error_reporting(E_ALL);
$myArray = array();
echo $myArray['foo']['bar'];
?>
关于php - undefined index 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6958472/