The is_iterable Function
The is_iterable
function checks whether a variable is iterable.
Arrays and objects implementing the Traversable
interface are considered iterable.
The function accepts one parameter - the variable to check, and returns true
or false
.
Syntax
is_iterable(mixed $value): bool
Example
Let's check an array for iterability:
<?php
$arr = [1, 2, 3];
var_dump(is_iterable($arr));
?>
Code execution result:
true
Example
Let's check a string for iterability:
<?php
$str = 'abc';
var_dump(is_iterable($str));
?>
Code execution result:
false
Example
Let's check an object implementing the Traversable
interface:
<?php
$obj = new ArrayObject([1, 2, 3]);
var_dump(is_iterable($obj));
?>
Code execution result:
true