加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 为什么第一次调用中没有E_NOTICE错误?

发布时间:2020-12-13 13:05:02 所属栏目:PHP教程 来源:网络整理
导读:我有以下代码段: error_reporting(E_ALL | E_STRICT); function getVal() { $data = []; return $data['hey']; //return $whatever; } function getVal2() { $data = []; return $data['hey']; } var_dump(getVal()); // No E_NOTICE error is issued - why
我有以下代码段:
error_reporting(E_ALL | E_STRICT);

    function &getVal() {
       $data = [];

       return $data['hey'];
       //return $whatever; 
    }

    function getVal2() {
       $data = [];

       return $data['hey'];
    }

    var_dump(getVal());  // No E_NOTICE error is issued - why?
    var_dump(getVal2()); // E_NOTICE error is issued.

问题是:为什么第一次调用中没有E_NOTICE错误?解释很可能是创建变量$data [‘hey’]来返回引用.但是,当$data [‘hey’](或$whatever,…)未定义时,发出E_NOTICE错误仍然是错误的.

这是预期的行为

http://www.php.net/manual/en/language.references.whatdo.php#language.references.whatdo.assign

If you assign,pass,or return an undefined variable by reference,it
will get created.

还有一些相关的“错误”:

https://bugs.php.net/bug.php?id=30350

Ok,it appears that the element is created because we are attempting
to return a reference to something that does not exist.

https://bugs.php.net/bug.php?id=27627

When you try to access a non-existant array element you effectively create it,hence the NULL entries in the array.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读