is_iterable()WP 4.9.6

Polyfill for is_iterable() function added in PHP 7.1.

Verify that the content of a variable is an array or an object implementing the Traversable interface.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.1.11, WP 4.9.8

No Hooks.

Return

true|false. True if $value is iterable, false otherwise.

Usage

is_iterable( $value );
$value(mixed) (required)
The value to check.

Examples

0

#1 Let us check the iterability of different values

var_dump( is_iterable([1, 2, 3]) );                     // bool(true)
var_dump( is_iterable(new ArrayIterator([1, 2, 3])) );  // bool(true)
var_dump( is_iterable((function () { yield 1; })()) );  // bool(true)

var_dump( is_iterable(1) );                             // bool(false)
var_dump( is_iterable(new stdClass()) );                // bool(false)

Changelog

Since 4.9.6 Introduced.

is_iterable() code WP 6.5.3

function is_iterable( $value ) {
	return ( is_array( $value ) || $value instanceof Traversable );
}