Home » Php » php – Why there is no E_NOTICE error in the first call?

php – Why there is no E_NOTICE error in the first call?

Posted by: admin July 12, 2020 Leave a comment


I have a following code snippet:

 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.

And the question is: Why there is no E_NOTICE error in the first call? The explanation is most likely that the variable $data['hey'] is created to return a reference. However, it still seems wrong not to issue an E_NOTICE error when $data['hey'] (or $whatever, ...) is not defined.

How to&Answers:

It’s expected behaviour


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

And some related “bugs”:


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


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


I think it is related to reference and its weaknesses in PHP.

When you reference in PHP the binding is created. PHP object typing is not strict and php doesn’t know what kind of object is referenced that is interpreter assumes that ['hey'] may exists in object.

PHP lacks of late binding and strict typing. That is why such “weird” things sometimes occur.


Use only error_reporting(1)
at the top of the php file…