4

As for for PHP >= 7.1 it is possible to detect if a variable is iterable or not using is_iterable().

is there an alternative to this for PHP <= 7 ?

how can i perform this since im working on php 7.0 ?

lotfio
  • 1,876
  • 2
  • 17
  • 33

1 Answers1

9

You just have to test, if the given var is of type Traversable or if it is an array. Everything else isn't iterable.

if (!function_exists('is_iterable')) {
    function is_iterable($var)
    {
        return is_array($var) || $var instanceof \Traversable;
    }
}
Philipp
  • 15,257
  • 4
  • 29
  • 47