PHP is_a() return false for array?

Viewed 113
var_dump(is_a([], "array"));

The above code displays "false".

But,

echo gettype([]);

Displays "array".

Am I doing something wrong or missing out on something here?

2 Answers

is_a() function checks if the object is of this class or has this class as one of its parents

To check if something is an array, you should use is_array().

Based on the question and what has been described in the comments, it sounds like you're looking for a way to identify if something (i.e an array or object) is iterable.

In PHP, this can be achieved with is_iterable(), which will return true if the passed-in parameter supports iteration via foreach.

From the docs:

Verify that the contents of a variable is accepted by the iterable pseudo-type, i.e. that it is either an array or an object implementing Traversable

In the case of the array you mentioned in the question:

is_iterable([]));

Outputs:

bool(true);
Related