I want to use a foreach loop with a variable, but this variable can be many different types, NULL for example.
foreach
NULL
So before foreach
You can check instance of Traversable with a simple function. This would work for all this of Iterator because Iterator extends Traversable
Traversable
Iterator
Iterator extends Traversable
function canLoop($mixed) { return is_array($mixed) || $mixed instanceof Traversable ? true : false; }