Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

is_countable

Checks whether a value can be passed to `count()`.

Syntax

is_countable(mixed $value): bool

Parameters

ParameterTypeDescription
$valuemixedThe 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.

Thanks for reading!

Donate to the site — we work hard for you.