is_iterable
Checks whether a value can be looped over with `foreach`.
Syntax
is_iterable(mixed $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $value | mixed | The value to check. |
Return value
bool`true` for an array and an object implementing `Traversable`.
Examples
An array, an iterator and a string:
<?php
var_dump(is_iterable([1, 2]));
var_dump(is_iterable(new ArrayIterator([1, 2])));
var_dump(is_iterable('text'));Output:
bool(true)
bool(true)
bool(false)Notes
In function parameters it is handier to declare the iterable type right away.
See also
- is_array — Checks whether a value is an array.
- iterator_to_array — Turns an iterator or a generator into an array.
- is_countable — Checks whether a value can be passed to `count()`.
In the tutorial
- Built-in interfaces — PHP interfaces that teach an object to behave like an array — Countable, IteratorAggregate, ArrayAccess, JsonSerializable and Stringable.