If you want to remove null values from an array, then passing them through a simple array_filter()
and using the built in isset()
function seems like a great option:
$dairyFree = true;
$toppings = [
'tomato',
'oregano',
'pepperoni',
($dairyFree) ? null : 'cheese',
];
$toppings = array_filter($toppings, 'isset');
Pretty simple, but using isset()
in array_filter()
raises the following warning:
Warning: array_filter() expects parameter 2 to be a valid callback, function 'isset' not found or invalid function name.
It is easy enough to wrap the same code inside a closure, but my question is – why would php balk at the isset()
function and not other internal functions like strlen()
?