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

前端之家收集整理的这篇文章主要介绍了php – 为什么第一次调用中没有E_NOTICE错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码段:
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.

原文链接:https://www.f2er.com/php/136239.html

猜你在找的PHP相关文章