Undefined Index Behavior

If I have an array in PHP that is currently zero, should access to the undefined index not contain an E_NOTICE level error?

If I have the following code snippet:

$myArray = null;
echo $myArray['foo']['bar'];

I would expect an error, but no problem. I checked that my log level is set to E_ALL. Is there something I am missing or is PHP happy to return null for undefined indexes until you try to modify the data?

+5
source share
2 answers

Yes, the undefined index only runs for non-null variables (don't ask me why). This will result in a notification:

<?php
    error_reporting(E_ALL);
    $myArray = array();
    echo $myArray['foo']['bar'];
?>
+3
source

, , $myArray null. , null, E_NOTICE. , , , .

+2

All Articles