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

phpis_iterable(mixed $value): bool

Example

Let's check an array for iterability:

php<?php
  $arr = [1, 2, 3];
  var_dump(is_iterable($arr));
?>

Code execution result:

phptrue

Example

Let's check a string for iterability:

php<?php
  $str = 'abc';
  var_dump(is_iterable($str));
?>

Code execution result:

phpfalse

Example

Let's check an object implementing the Traversable interface:

php<?php
  $obj = new ArrayObject([1, 2, 3]);
  var_dump(is_iterable($obj));
?>

Code execution result:

phptrue

See Also

  • the is_array function,
    which checks whether a variable is an array
  • the is_object function,
    which checks whether a variable is an object