In this article, you will learn how to check if a variable contains iterable value or not in PHP. Iterable value in PHP is a value on which we can implement loop.
The is_iterable() function in PHP checks whether the contents of a variable are an iterable value or not. This function returns true (1) if the variable is iterable, otherwise it returns false/nothing.
what is the syntax of the IS_ITERABLE() function in php?
is_iterable(variable);
Parameter | Description |
---|---|
variable | Required. Specifies the variable to check |
examples of the IS_ITERABLE() function
Example 1. In this example, we check whether the contents of a variable are an iterable value or not.
<?php
$a = "Hello";
echo "a is " . is_iterable($a) . "<br>";
$b = array("red", "green", "blue");
echo "b is " . is_iterable($b) . "<br>";
$c = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "c is " . is_iterable($c) . "<br>";
$d = [1, 2, 3];
echo "d is " . is_iterable($d) . "<br>";
?>