I came across a code snippet which included $a = & $b;
but hadn\'t tested whether $b actually existed (if (isset($b))
). I wasn\'t sure how PHP
Explanation is as simple as this
If you assign, pass, or return an undefined variable by reference, it will get created.
(emphasis mine)
That's what you're doing; Assigning an undefined index by reference so it gets created.
Example #1 Using references with undefined variables
<?php
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null
$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)
$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
Example from PHP Manual
Then you have another question:
Meanwhile at the same time, $b[11] shows a value NULL and isset()=FALSE even though its referent (apparently) does exist (!)
That is also explained clearly on the manual
isset — Determine if a variable is set and is not NULL
isset() will return FALSE if testing a variable that has been set to NULL
Since it is NULL
, isset()
returns FALSE.
The slot has to exist for you to be able to alias another variable to it (which is really what's going on here, PHP's "references" aren't really actual things as much as each "by reference" operation is a regular operation with copying replaced by aliasing), but it doesn't have to contain a non-null value, and doesn't until you assign it one (through either of its names).