W3docs

is_array()

The is_array() function is a built-in function in PHP that checks whether a variable is an array or not. An array is a special type of variable that holds a

What is is_array()?

is_array() is a built-in PHP function that checks whether a given variable is an array. It returns true if the variable is an array and false for every other type (string, integer, object, null, and so on).

PHP is dynamically typed, so a variable can hold any kind of value at runtime. Before you loop over a value, index into it, or pass it to an array function like count() or array_map(), it is often worth confirming that you really have an array. Calling an array function on a non-array can throw a TypeError (PHP 8+) or emit a warning, so a quick is_array() guard makes your code safer.

Syntax

is_array(mixed $value): bool

It takes a single parameter, $value — the variable to test — and always returns a boolean. It never raises an error of its own, so it is safe to call on any value, including unset-looking results from functions.

Basic example

php— editable, runs on the server

Here $list is an array, so is_array() returns true; $text is a string, so it returns false. We use var_dump() instead of echo because echo true prints 1 while echo false prints an empty string — which makes booleans hard to read. var_dump() shows the type and value explicitly.

What counts as an array

Both indexed and associative arrays return true, and so does an empty array. Anything that merely looks like a collection — such as an object or an ArrayObject — does not count as a native array.

<?php
var_dump(is_array([]));                 // bool(true)  — empty array
var_dump(is_array([1, 2, 3]));          // bool(true)  — indexed
var_dump(is_array(["name" => "Ann"]));  // bool(true)  — associative
var_dump(is_array([[1, 2], [3, 4]]));   // bool(true)  — multidimensional

var_dump(is_array("array"));            // bool(false) — string
var_dump(is_array(42));                 // bool(false) — integer
var_dump(is_array(null));               // bool(false) — null
var_dump(is_array(new stdClass()));     // bool(false) — object
?>

Practical use: guarding before iteration

A common pattern is normalizing input that may arrive as either a single value or a list, then iterating safely:

<?php
function printAll(mixed $input): void
{
    // Wrap a single value in an array so the loop always works.
    if (!is_array($input)) {
        $input = [$input];
    }

    foreach ($input as $item) {
        echo $item, "\n";
    }
}

printAll("just one");        // just one
printAll(["a", "b", "c"]);   // a / b / c on separate lines
?>

This avoids a foreach warning when $input is a scalar, and keeps the calling code simple.

  • is_array() vs. gettype(): gettype() returns a string such as "array"; is_array() returns a boolean directly, which reads better in an if.
  • is_array() vs. is_iterable(): if you only need to know whether you can foreach a value, is_iterable() is broader — it accepts both arrays and Traversable objects (like generators).
  • is_array() vs. is_countable(): use is_countable() before calling count() so you also accept objects implementing the Countable interface.
  • For other scalar checks see is_string() and is_int().

Summary

Use is_array() whenever you need a quick, error-free check that a value is a true PHP array — most often as a guard before looping or before calling an array-specific function. If you need to allow iterable objects as well, reach for is_iterable() or is_countable() instead.

Practice

Practice
What does the 'is_array' function in PHP do?
What does the 'is_array' function in PHP do?
Was this page helpful?