is_countable()
The is_countable() function is a built-in function in PHP that checks if a variable is countable. It was introduced in PHP 7.3 as a language construct, which
Introduction
The is_countable() function is a built-in PHP function that checks whether a variable is countable — that is, whether it can safely be passed to count(). A value is countable when it is an array or an object that implements the Countable interface. The function returns true for those two cases and false for everything else.
It was introduced in PHP 7.3 to solve a real problem. Starting in PHP 7.2, calling count() on a value that is not an array or Countable object emits a warning (and in PHP 8.0 it became a TypeError). Before is_countable() existed, you had to write is_array($x) || $x instanceof Countable by hand to guard against that. This function makes that check a single, readable call.
This page covers the syntax, return value, a working example, the common "safe count" pattern, and the gotchas to watch for.
Syntax
The syntax of the is_countable() function is as follows:
The PHP syntax of the is_countable()
mixed is_countable(mixed $var)Note: The mixed type hint requires PHP 8.0+. In PHP 7.3, the signature is simply is_countable($var).
The function takes a single parameter, $var, the variable to check.
Return value
is_countable() always returns a boolean and never raises an error, regardless of what you pass it:
$var | Returns |
|---|---|
Array (e.g. [1, 2, 3] or []) | true |
Object implementing Countable | true |
| String, integer, float, boolean | false |
null | false |
Plain object (e.g. new stdClass()) | false |
The empty array [] is still countable — being countable is about the type, not whether the value holds any elements.
Example Usage
Here is an example of how to use the is_countable() function in PHP:
Example of PHP is_countable()
<?php
$var1 = ["apple", "banana", "orange"];
$var2 = "hello";
$var3 = new stdClass();
$var4 = 42;
var_dump(is_countable($var1)); // output: bool(true)
var_dump(is_countable($var2)); // output: bool(false)
var_dump(is_countable($var3)); // output: bool(false)
var_dump(is_countable($var4)); // output: bool(false)
class MyCountable implements Countable {
public function count(): int { return 5; }
}
$countableObj = new MyCountable();
var_dump(is_countable($countableObj)); // output: bool(true)
?>In this example, we define four variables with different data types: $var1 is an array, $var2 is a string, $var3 is a plain object, and $var4 is an integer. We then use is_countable() to check each one. Only $var1 (an array) is countable. Note that $var3 is false even though it is an object — a plain stdClass does not implement Countable. The final example shows that MyCountable, which implements the Countable interface, is recognized as countable.
The "safe count" pattern
The most common reason to reach for is_countable() is to guard a count() call against values that might not be countable — for instance data coming from an API, a database, or user input where a field could be null, a string, or a missing array:
Guarding count() with is_countable()
<?php
function safeCount(mixed $value): int {
return is_countable($value) ? count($value) : 0;
}
echo safeCount(["a", "b", "c"]), "\n"; // output: 3
echo safeCount("not an array"), "\n"; // output: 0
echo safeCount(null), "\n"; // output: 0
?>Without the guard, count("not an array") throws a TypeError in PHP 8.0+. is_countable() lets you fall back to a sensible default instead of crashing.
Gotchas
- A plain object is not countable.
is_countable(new stdClass())isfalse. The object must implement theCountableinterface for the check to pass. - Strings are never countable, even though they have a length. Use
strlen()for characters, notcount(). - It only checks the type, not depth.
is_countable()reports nothing about nested arrays —count()itself takes a$modeargument for recursive counting. - Before PHP 7.3 you can replicate the check with
is_array($var) || $var instanceof Countable.
Related functions
count()— counts the elements of a countable value; the functionis_countable()is meant to protect.is_array()— narrower check for the array case only.is_object()— check whether a value is any object (countable or not).gettype()— get the type name of a variable.
Conclusion
The is_countable() function is a useful tool for checking if a variable is countable in PHP. It can be used to avoid errors that may occur when trying to count non-countable variables, such as strings or objects. By using this function, developers can ensure that their code is working with countable variables only, making their code more efficient and reliable.