$stuff = null;
foreach ($stuff as $item) {
var_dump($item);
}
you would be greeted with an "Invalid argument supplied for foreach()" warning. So it's common to see the extra code that the author mentions: $stuff = null;
if (! empty($stuff)) {
foreach ($stuff as $item) {
var_dump($item);
}
}
You can get around this by using a null coalesce (??) or a ternary shorthand (?:) for null or boolean values respectively. This shows the former which gets closer to what the author describes for this situation, though it may sacrifice some readability under certain situations: $stuff = null;
foreach ($stuff ?? [] as $item) {
var_dump($item);
}But it happens anyway sometimes, of course :)... in Python, I've also often used the `or` shortcut for that, like this
for item in stuff or []:
var_dump(item)
or just at the top of the function, reassigning the function argument: stuff = stuff or []
[1] https://en.wikipedia.org/wiki/Null_object_pattern