The is_array
method is a built-in function in PHP used to check whether a variable is an array.
is_array(mixed $variable_to_test): bool
The function takes in one parameter, variable_to_test
.
The is_array
method returns true
if the passed argument is an array; otherwise, it returns false
.
<?$arr=array('A', 'B', 'C');print_r($arr);echo is_array($arr) ? ' is an Array' : ' is not an Array';echo "\n\n-----------\n";$arr = array('odd' => array(3, 5), 'even' => array(2, 4, 6));print_r($arr);echo is_array($arr) ? ' is an Array' : ' is not an Array';echo "\n\n-----------\n";$num = 67.099;print($num);echo is_array($num) ? ' is an Array' : ' is not an Array';echo "\n\n-----------\n";$str= "hello";print($str);echo is_array($str) ? ' is an Array' : ' is not an Array';?>
In the code above, we call the is_array
method on $arr
.
$arr=array('A', 'B', 'C');
is_array($arr)
For the variable $arr
, the is_array
method returns true
because $arr
is an array.
$arr = array(
'odd' => array(3,5),
'even' => array(2,4,6)
);
is_array($arr);
For the value of $arr
above, the is_array
method returns true
because $arr
is a multidimensional array.
$num = 67.099;
is_array($num);
For the above value of $num
, the is_array
method returns false
because $num
is not an array.
$str= "hello";
is_array($str);
For the value of $num
, the is_array
method returns false
because $num
is a string, not an array.