The is_array Function
The is_array
function checks if the passed variable is an array.
Returns true
if the variable is an array, and false
otherwise.
The function takes one parameter - the variable to check.
Syntax
is_array(mixed $var): bool
Example
Check if a variable is an array:
<?php
$arr = [1, 2, 3];
$res = is_array($arr);
var_dump($res);
?>
Code execution result:
true
Example
Check a string for being an array:
<?php
$str = 'abcde';
$res = is_array($str);
var_dump($res);
?>
Code execution result:
false
Example
Check several different variable types:
<?php
$values = [
[1, 2, 3],
'string',
123,
null,
new stdClass()
];
foreach ($values as $value) {
echo gettype($value) . ': ';
var_dump(is_array($value));
echo "\n";
}
?>
Code execution result:
array: true
string: false
integer: false
NULL: false
object: false