Definition and Usage
- PHP Version
- 4+
The is_array() function checks whether a given value is an array.
It returns true if the value passed as an argument is an array; otherwise, it returns false.
The array type is
a data type used to represent an array.
In PHP, arrays consist of two types: indexed arrays and associative arrays.
Basic Example
/* Indexed array type */
var_dump(is_array(array(1, 2, 3))); // bool(true)
var_dump(is_array([1, 2, 3])); // bool(true)
var_dump(is_array([])); // bool(true)
/* Associative array type */
var_dump(is_array(array('name' => 'foo', 'age' => 30))); // bool(true)
var_dump(is_array(['name' => 'foo', 'age' => 30])); // bool(true)
/* Boolean and string types */
var_dump(is_array(true)); // bool(false)
var_dump(is_array(false)); // bool(false)
var_dump(is_array('array')); // bool(false)
/* Other types */
var_dump(is_array(100)); // bool(false)
var_dump(is_array(null)); // bool(false)
/* Type casting */
var_dump(is_array((array) 'foo')); // bool(true)
var_dump(is_array((array) 100)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_array(mixed $value): bool
Parameters
$value |
Required. The value to check. |
|---|
Return Values
If the value passed as an argument is an array, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_array() function.
Empty Arrays Also Return true
The is_array() function returns true for any variable that represents an array, including empty arrays. This is because the function specifically checks whether the variable is of the array type. Since an empty array is still fundamentally an array, the function recognizes it as such and returns true.
$a = array(); // Empty array using array()
$b = []; // Empty array using [] (introduced in PHP 5.4+)
var_dump(is_array($a)); // bool(true)
var_dump(is_array($b)); // bool(true)
Objects Are Not Considered Arrays
Although objects can behave in a manner similar to arrays, they are not identical. Objects provide significantly more functionality and possess various characteristics that differ from those of arrays. Therefore, it is important to remember that the is_array() function will always return false when checking an object type.
To verify if a variable is an object, it is recommended to use the is_object() function.
class MyClass {
public $property;
public function __construct($value) {
$this->property = $value;
}
}
$obj = new MyClass('value');
var_dump(is_array($obj)); // bool(false)
Code Explanation
The is_object() function checks whether a given value is of the object type.
If the value passed as a parameter is an object, the function returns true; otherwise, it returns false.
Practical Examples
The is_array() function is useful in the following scenarios:
Improving Code Reliability
When you need to ensure that a specific variable is an array, you can use the is_array() function to perform a safe validation. This helps prevent errors that may occur when a variable of an unexpected type is passed.
function my_function($data) {
if (!is_array($data)) {
throw new InvalidArgumentException('The argument must be an array.');
}
// ...
}
In the code above, my_function() requires an argument named $data. If a data type other than an array is passed when the function is called, an exception is thrown to prevent the function from behaving unexpectedly.
Validating Before Executing Array Functions
It is recommended to use the is_array() function to verify that a variable is indeed an array before performing array-related operations. This approach prevents runtime errors and improves code readability.
$data = maybe_get_array();
if (is_array($data)) {
foreach ($data as $item) {
// ...
}
}
Code Explanation
The foreach() loop is a fundamental construct used to iterate over arrays and objects for repetitive processing.
In the example above, assume the $data variable holds a value returned by the maybe_get_array() function. Since the returned value might not be an array, the is_array() function is used to confirm the type before proceeding.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_int() Function – Checking Whether a Value Is an Integer
- PHP is_float() Function – Checking Whether a Value Is a Float
- PHP is_numeric() Function – Checking Whether a Value Is a Number or a Numeric String
- PHP is_string() Function – Checking Whether a Value Is a String
- PHP is_bool() Function – Checking Whether a Value Is a Boolean
- PHP is_object() Function – Checking Whether a Value Is an Object
- PHP is_resource() Function – Checking Whether a Value Is a Resource
- PHP is_null() Function – Checking Whether a Value Is NULL
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide