I came across some PHP behavior that I think is subtle, but pretty cool. But I don’t understand how…
$test=array('a'=>'c', 'b'=>'c');
unset($test['a']);
var_dump($test);
This prints
array(1) { ["b"]=> string(1) "c" }
I would have expected the array to be emptied out. After all, $test[‘a’] evaluates to ‘c’ so the unset function only sees ‘c’ but knows it was just the first ‘c’ value I wanted removing?
My guess is the interpeter is super smart and looks inside the array inside the parameter given to it – but that’s purely conjecture …
Erm, no.
unsetis not a function, it is a language construct. Therefore it doesn’t necessarily follow the same rules.In this case, however, it actually works similarly to a pass-by-reference. It takes the reference to the variable, and destroys it.