I’ve been thinking of using reference assignment as a shortcut for dealing with potentially undefined variables.
In other words, instead of:
$foo = isset($this->blah['something']['else']) ? $this->blah['something']['else'] : null;
if (!is_null($foo) && ...){
//do something with $foo
}
I could do this:
$foo = &$this->blah['something']['else'];
if (!is_null($foo) && ...){
//do something with $foo
}
Seems simpler, right? Because of the way PHP handles assignment by reference, I don’t have to worry about $this->blah[‘something’][‘else’] being defined, because if it doesn’t exist it will be created automatically and set to NULL.
Is this strategy frowned upon?
If you just want to test if a variable is set and not null, then you better test:
This way you avoid creating two references to a value that could not even exist.
In my opinion, every time you create a new reference to the same value, your code becomes harder to understand at a glance.
If you need zero as non-empty, then you better create a global function like this: