is_iterable()

is_iterable() is a PHP native function, introduced in version 7.1, that checks whether a variable can be iterated over with foreach. It returns true if the value is either an array or an object implementing the Traversable interface, such as a Generator or an Iterator.

is_iterable() is the runtime counterpart of the iterable pseudo-type, which may be used in type declarations for parameters, return types, and properties. It is looser than is_array(), since it also accepts objects, but it does not accept plain objects that merely implement ArrayAccess without also being Traversable.

It is typically used to validate arguments of functions designed to accept both arrays and generators or iterators interchangeably, before looping over them with foreach.

<?php

    function process(iterable $items): void {
        foreach ($items as $item) {
            echo $item . PHP_EOL;
        }
    }

    var_dump(is_iterable([1, 2, 3]));           // true
    var_dump(is_iterable((function () { yield 1; })())); // true
    var_dump(is_iterable('hello'));             // false
    var_dump(is_iterable(new ArrayObject([1]))); // true

?>

Documentation

See also Type comparison tables.

Related : Iterable, Traversable, Iterator, Generator, is_array(), is_object(), gettype(), Type Checking

Added in PHP 7.1