Why is there no E_NOTICE error in the first call?

I have the 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 is there no E_NOTICE error in the first call? The explanation is most likely that the variable $data['hey'] created to return the link. However, it still seems wrong to not throw an E_NOTICE error if $data['hey'] (or $whatever , ... ) is not defined.

+8
php
source share
3 answers

Expected Behavior

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 be created.

And some related "errors":

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

Well, it looks like the element was created because we are trying to return a link to something that does not exist.

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

When you try to access a nonexistent element of an array, you effectively create it, therefore, NULL entries in the array.

+1
source share

I think this is due to the link and its flaws in PHP.

When you reference in PHP, a binding is created. Setting up PHP objects is not strict, and php does not know which object refers to, that the interpreter assumes that ['hey'] can exist in the object.

PHP lacks late binding and strong typing. That is why such "strange" things sometimes happen.

0
source share

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

-3
source share

All Articles