PHP is_iterable() Function
The PHP is_iterable() function checks whether a variable is an iterable value or not. The function returns true if the variable is an iterable value, otherwise it returns false.
Syntax
is_iterable(variable)
Parameters
variable |
Required. Specify the variable being evaluated. |
Return Value
Returns true if variable is an iterable value, false otherwise.
Example:
The example below shows the usage of is_iterable() function.
<?php $x = array(1, 2, 3); $y = ['a', 'b', 'c']; $z = array(10=>'Red', 20=>'Green', 30=>'Blue'); var_dump(is_iterable($x)); //returns: bool(true) var_dump(is_iterable($y)); //returns: bool(true) var_dump(is_iterable($z)); //returns: bool(true) var_dump(is_iterable(new ArrayIterator($x))); //returns: bool(true) var_dump(is_iterable(new ArrayIterator())); //returns: bool(true) var_dump(is_iterable((function(){yield 10;})())); //returns: bool(true) echo "\n"; var_dump(is_iterable(10)); //returns: bool(false) var_dump(is_iterable('xyz')); //returns: bool(false) var_dump(is_iterable(true)); //returns: bool(false) var_dump(is_iterable(new stdClass())); //returns: bool(false) ?>
The output of the above code will be:
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(false) bool(false)
❮ PHP Variable Handling Reference