PHP is_array() Function

What is PHP is_array() Function?

is_array() function can tell you whether a variable is an array or not.

No matter whether the defined array is a simple array or associative array or multi-dimensional array or empty array, the function considers all these as arrays. So, if a variable has capability to store collection of data which is the characteristic of an array, the is_array() function treats this variable as an array.

Syntax:

is_array(variable)

Parameters:

This function takes only one parameter which is required.

variable: The variable to check whether it is array or not.

Return Values:

The function returns Boolean value TRUE or FALSE. It returns-

  • TRUE (or 1) – If the variable is an array.
  • FALSE (or 0)- If the variable is not an array.

Examples:

Example 1:

<?php
$var1 = "whether an array or not";
var_dump(is_array($var1));

echo "<br />";

$var2 = 100;
var_dump(is_array($var2));

echo "<br />";

$var3 = TRUE;
var_dump(is_array($var3));

echo "<br />";

$var4 = "";
var_dump(is_array($var4));

echo "<br />";

$var4 = NULL;
var_dump(is_array($var4));
?>

Output:

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

Example 2:

<?php
$var1 = array();
var_dump( is_array($var1));
echo "<br />";
echo is_array($var1);

echo "<br />";

$var2 = array(25, 15, -999);
echo is_array($var2);

echo "<br />";

$var3 = array("one"=>"First", "Two"=>"Second", "Three"=>"Third");
echo is_array($var3);

echo "<br />";

$var4 = array(
    array(1,2,3),
    array(4,5,6)
);
echo is_array($var4);
?>

Output:

bool(true)
1
1
1
1

Practical Usages of is_array() Function:

Before performing array related operations on a variable considering it as an array, you can use this is_array() function to ensure its type.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_array() Function

is_array() is a variable handling type function. it is a simple but effect method to check whether a variable is a array or not.

Reference:

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