I usually use a construct similar to this:
Determine if a variable is iterable. i.e. can be used to loop over.
*
* @return bool
/
function is_iterable($var)
{
return $var !== null
&& (is_array($var)
|| $var instanceof Traversable
|| $var instanceof Iterator
|| $var instanceof IteratorAggregate
);
}
$values = get_values();
if (is_iterable($values))
{
foreach ($values as $value)
{
// do stuff...
}
}
Note that this particular version is not tested, it's typed directly into SO from memory.