5

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,916
  • 2
  • 18
  • 34

1 Answers1

10

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,377
  • 4
  • 35
  • 52