The is_iterable() function is an inbuilt function in PHP which is used to check whether the contents of a variable is an iterable value or not.
Syntax:
bool is_iterable( mixed $var )
Parameters: This function accepts single parameter as mentioned above and described below:
- $var: It contains the value of variable that need to be check.
Return Value: It returns TRUE if the value of variable is iterable, FALSE otherwise.
Program 1:
<?php // Declare an array $arr = array (1, 2, 3, 4, 5);
if (is_iterable( $arr )) {
echo "Array is iterable" ;
} else {
echo "Array is not iterable" ;
} // Create a class class GFG {
} // Create an object $obj = new GFG();
if (is_iterable( $obj )) {
echo "\nObject is iterable" ;
} else {
echo "\nObject is not iterable" ;
} ?> |
Output:
Array is iterable Object is not iterable
Program 2:
<?php // Create a class class GFG {
public $Geek_name = "Welcome to GeeksforGeeks" ;
} $obj = new GFG();
var_dump(is_iterable( $obj ));
$arr = array ( 'G' , 'e' , 'e' , 'k' , 's' );
var_dump(is_iterable( $arr ));
$num = 25;
var_dump(is_iterable( $num ));
$str = "GeeksforGeeks" ;
var_dump(is_iterable( $str ));
$bool = true;
var_dump(is_iterable( $bool ));
?> |
Output:
bool(false) bool(true) bool(false) bool(false) bool(false)
Reference: https://www.php.net/manual/en/function.is-iterable.php