Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

is_iterable

Checks whether a value can be looped over with `foreach`.

Syntax

is_iterable(mixed $value): bool

Parameters

ParameterTypeDescription
$valuemixedThe 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

In the tutorial

  • Built-in interfaces — PHP interfaces that teach an object to behave like an array — Countable, IteratorAggregate, ArrayAccess, JsonSerializable and Stringable.

Thanks for reading!

Donate to the site — we work hard for you.