PHP | is_iterable() Function

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
}
?>

chevron_right


Output:

Array is iterable
Object is not iterable

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


Output:

bool(false) 
bool(true) 
bool(false) 
bool(false) 
bool(false) 

Reference: https://www.php.net/manual/en/function.is-iterable.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.