Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for iterable content PHP

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 ?

like image 810
lotfio Avatar asked Feb 05 '23 13:02

lotfio


1 Answers

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;
    }
}
like image 143
Philipp Avatar answered Feb 08 '23 15:02

Philipp