PHP is_countable() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, is_countable() checks whether a value can be used with count(). It returns true for arrays and for objects that implement the Countable interface (added in PHP 7.3).

Since PHP 8.0, calling count() on a value that is not countable throws a TypeError. is_countable() lets you check first, so your code stays safe when the input might be a string, a number or null.

Syntax

Call is_countable() with the values you need. Start with this line.

PHP
$result = is_countable($value);
CallWhat it does
is_countable([1, 2, 3])Returns true, because arrays can be counted.
is_countable(new ArrayObject([1]))Returns true, because ArrayObject implements Countable.
is_countable("hello")Returns false. Use strlen() to measure a string.

is_countable() needs PHP 7.3 or newer. Generators and other iterators are not countable unless they also implement Countable, so is_countable() is false for them even though you can loop over them.

Library declarations
PHP
is_countable(mixed $value): bool

Parameters

ParameterDescription
valueRequired. The value to test. It can be any type.

Return Value

Return ValueDescription
booltrue if the value is an array or an object that implements Countable, otherwise false.

PHP Examples

Example 1 — Countable and Not Countable

Check an array, an object, a string and a number.

PHP
<?php
var_dump(is_countable([1, 2, 3]));
var_dump(is_countable(new ArrayObject([1])));
var_dump(is_countable("abc"));
var_dump(is_countable(42));

How It Works

  1. Every array can be counted, so the first call is true.
  2. ArrayObject implements the Countable interface, so it is also countable.
  3. A string is not countable. Use strlen() for its length.
  4. A number cannot be counted either.

Example 2 — A Safe Count Function

Wrap count() in a small function that returns 0 instead of throwing an error.

PHP
<?php
function safeCount($value) {
    return is_countable($value) ? count($value) : 0;
}

echo safeCount([1, 2, 3]), PHP_EOL;
echo safeCount(new ArrayObject([1, 2])), PHP_EOL;
echo safeCount("hello"), PHP_EOL;
echo safeCount(null), PHP_EOL;

How It Works

  1. safeCount() first asks is_countable() whether count() is allowed.
  2. For an array with three items, count() returns 3.
  3. For an ArrayObject with two items, count() returns 2.
  4. A string and null are not countable, so the function returns 0 and never reaches count().

Example 3 — Custom Countable Classes and Generators

Your own classes can be countable, but generators cannot be counted even though they can be looped over.

PHP
<?php
class Basket implements Countable {
    public function count(): int {
        return 4;
    }
}

var_dump(is_countable(new Basket()));
echo count(new Basket()), PHP_EOL;

$gen = (function () {
    yield 1;
    yield 2;
})();

var_dump(is_countable($gen));
var_dump(is_iterable($gen));

How It Works

  1. Basket implements the Countable interface and defines a count() method.
  2. That makes is_countable() true, and count(new Basket()) returns 4.
  3. A generator produces values one by one and has no fixed size, so it is not countable.
  4. A generator can still be looped over, so is_iterable() returns true.

Frequently Asked Questions

It returns true if a value is an array or an object implementing Countable, which means count() can be used on it safely.
PHP 7.3. In older versions, developers wrote is_array($x) || $x instanceof Countable.
Since PHP 8.0, count() only accepts arrays and Countable objects. Passing null, a string or a number throws a TypeError. is_countable() lets you check first.
is_countable() is true when count() works. is_iterable() is true when foreach works. A generator is iterable but not countable, and a plain array is both.
Implement the Countable interface and add a public count(): int method. After that, count($object) calls your method and is_countable() returns true.

Next: is_double()

is_double() is an alias of is_float(). It checks whether a variable is a floating-point nu.

is_double() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful