PHP is_iterable() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. The value you want to check. It can be of any type.

Return Value

Return ValueDescription
boolReturns 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.

PHP
<?php
var_dump(is_iterable([1, 2, 3]));
var_dump(is_iterable('hello'));
var_dump(is_iterable(42));

How It Works

  1. The array is iterable. Every PHP array can be looped over with foreach, so the result is true.
  2. The string is not iterable. A string is a single value, so is_iterable() returns false.
  3. The number is not iterable. An integer has no items to loop over, so the result is false.
  4. 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));

How It Works

  1. The Box object is not Traversable. It is a plain object, so is_iterable() returns false.
  2. ArrayIterator is Traversable. It is built to be looped over, so the result is true.
  3. Calling a generator function returns a Generator object. A Generator is Traversable, so it is iterable.
  4. 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');

How It Works

  1. The function accepts any value. The parameter has no type, so a caller can pass anything.
  2. The guard runs first. If is_iterable() returns false, the function prints a message and stops with return.
  3. The first call passes an array. The check passes, so foreach prints red and then green.
  4. 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.

Next: is_long()

is_long() checks whether a value is an integer. It is an alias of is_int(), so both functi.

is_long() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful