Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | is_iterable() Function

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:




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

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments