is_countable
Checks whether a value can be passed to `count()`.
Syntax
is_countable(mixed $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $value | mixed | The value to check. |
Return value
bool`true` for an array and an object implementing `Countable`.
Examples
An array, an object and null:
<?php
var_dump(is_countable([1, 2]));
var_dump(is_countable(new ArrayObject()));
var_dump(is_countable(null));Output:
bool(true)
bool(true)
bool(false)Notes
count(null) and count('text') throw a TypeError — the check helps avoid it.
See also
- count — Returns the number of elements in an array.
- is_array — Checks whether a value is an array.
- is_iterable — Checks whether a value can be looped over with `foreach`.
In the tutorial
- Built-in interfaces — PHP interfaces that teach an object to behave like an array — Countable, IteratorAggregate, ArrayAccess, JsonSerializable and Stringable.