return empty array in php

I would recommend declaring $arr = array(); at the very top of the function so you don't have to worry about it.

If you are doing the check immediately before you return, I do not recommend isset. The way you are using foreach is depending on an array being returned. If $arr is set to a number, for example, then it will still validate. You should also check is_array(). For example:

if (isset($arr) && is_array($arr))
    return $arr;
else 
    return array();

Or in one line instead:

return (isset($arr) && is_array($arr)) ? $arr : array();

But, like I said, I recommending declaring the array at the very top instead. It's easier and you won't have to worry about it.


To avoid complexity, Simply

return [];

Tags:

Php

Arrays