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

phpis_array(mixed $var): bool

Example

Check if a variable is an array:

php<?php
  $arr = [1, 2, 3];
  $res = is_array($arr);
  var_dump($res);
?>

Code execution result:

phptrue

Example

Check a string for being an array:

php<?php
  $str = 'abcde';
  $res = is_array($str);
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Check several different variable types:

php<?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:

phparray: true
string: false
integer: false
NULL: false
object: false

See Also

  • the is_string function,
    which checks if a variable is a string
  • the is_int function,
    which checks if a variable is an integer
  • the is_object function,
    which checks if a variable is an object