In PHP, is_iterable() checks whether a value can be looped over with foreach. It returns true for arrays and for objects that implement the Traversable interface, such as iterators and generators.
Use it before a foreach loop when a variable might hold something that cannot be looped, like a string, a number or null. It was added in PHP 7.1 and gives you a simple yes or no answer without any warnings.
Syntax
Call is_iterable() with the values you need. Start with this line.
PHP
$ok = is_iterable($value);
Call
What it does
is_iterable([1, 2, 3])
Returns true because every array can be used in foreach.
is_iterable('hello')
Returns false because a string is not iterable in PHP.
is_iterable(new ArrayIterator([1, 2]))
Returns true because ArrayIterator implements Traversable.
Generators (functions that use yield) are iterable too, because a generator object is Traversable. Plain objects are not, even though foreach can loop over their public properties.
Library declarations
PHP
is_iterable(mixed $value): bool
Parameters
Parameter
Description
value
Required. The value you want to check. It can be of any type.
Return Value
Return Value
Description
bool
Returns true if the value is an array or a Traversable object, and false for everything else.
PHP Examples
Example 1 — Checking Common Values
Test an array, a string and a number to see which ones can be used in a foreach loop.
The array is iterable. Every PHP array can be looped over with foreach, so the result is true.
The string is not iterable. A string is a single value, so is_iterable() returns false.
The number is not iterable. An integer has no items to loop over, so the result is false.
var_dump() prints each result. It shows bool(true) or bool(false) so you see the exact type.
Example 2 — Objects, Iterators and Generators
Objects are only iterable when they are Traversable. See how a plain object, an iterator, a generator and null behave.
PHP
<?php
class Box {
public $a = 1;
}
function numbers() {
yield 1;
yield 2;
}
var_dump(is_iterable(new Box()));
var_dump(is_iterable(new ArrayIterator([1, 2])));
var_dump(is_iterable(numbers()));
var_dump(is_iterable(null));
Output
bool(false)
bool(true)
bool(true)
bool(false)
How It Works
The Box object is not Traversable. It is a plain object, so is_iterable() returns false.
ArrayIterator is Traversable. It is built to be looped over, so the result is true.
Calling a generator function returns a Generator object. A Generator is Traversable, so it is iterable.
null is not iterable. It holds no items, so the last result is false.
Example 3 — A Safe Loop Helper
Write a small function that only loops when the input can be looped, and prints a message otherwise.
PHP
<?php
function printAll($items) {
if (!is_iterable($items)) {
echo 'Not iterable', PHP_EOL;
return;
}
foreach ($items as $item) {
echo $item, PHP_EOL;
}
}
printAll(['red', 'green']);
printAll('blue');
Output
red
green
Not iterable
How It Works
The function accepts any value. The parameter has no type, so a caller can pass anything.
The guard runs first. If is_iterable() returns false, the function prints a message and stops with return.
The first call passes an array. The check passes, so foreach prints red and then green.
The second call passes a string. The check fails, so it prints Not iterable and nothing is looped.
Frequently Asked Questions
It checks whether a value can be used in a foreach loop. It returns true for arrays and Traversable objects, and false for strings, numbers, booleans, null and plain objects.
No. A string is a single value, so is_iterable('hello') returns false. If you want to loop over its characters, use str_split() or a for loop with indexes.
Yes. A generator function returns a Generator object, which implements Traversable, so is_iterable() returns true for it.
is_array() only returns true for real arrays. is_iterable() also returns true for objects that implement Traversable, such as ArrayIterator and generators, so it covers more loopable values.
PHP 7.1 added is_iterable() together with the iterable type declaration. It is available in every modern version of PHP.