I am curious to know if the following behavior is intended in PHP or not. And, if this is assumed, it is considered acceptable to initialize an array from a null variable by creating an index into it (as is done in the first code fragment)?
error_reporting(E_ALL); $arr = null; echo ($arr["blah"]===null) ? "null" : $arr["blah"]; $arr["blah"] = "somevalue"; echo "<br>"; echo ($arr["blah"]===null) ? "null" : $arr["blah"]; var_dump ($arr);
Displays
null somevalue array (size=1) 'blah' => string 'somevalue' (length=9)
However, if the array is initialized first (see the code below), I get the same result, but the notification "Undefined Index" is indicated on the first attempt $arr["blah"]
error_reporting(E_ALL); $arr = array(); echo ($arr["blah"]===null) ? "null" : $arr["blah"]; $arr["blah"] = "somevalue"; echo "<br>"; echo ($arr["blah"]===null) ? "null" : $arr["blah"]; var_dump ($arr);
source share