Gabriel Caruso

Gabriel Caruso


PHP RFC: is_countable


In PHP 7.2, a Warning was added while trying to count uncountable things. After that, everyone was forced to search and change their code, to avoid it. Usually, the following piece of code became standard:

if (is_array($foo) || $foo instanceof Countable) {
    // $foo is countable

This condition, to check if a variable “is countable”, is also very common in methods that return the count of the elements:

if (is_array($foo) || $foo instanceof Countable) {
    return count($foo);


This RFC proposes a new type function, that returns true if the given value is an array type or an instance of the Countable interface.


if (is_array($foo) || $foo instanceof Countable) {
    // $foo is countable


if (is_countable($foo)) {
    // $foo is countable



bool is_countable(mixed $var)

Verify that the content of a variable is an array or an object implementing Countable



The value to check

Return Values

Returns TRUE if var is countable, FALSE otherwise


Example #1: is_countable

var_dump(is_countable([1, 2, 3])); // bool(true)
var_dump(is_countable(new ArrayIterator(['foo', 'bar', 'baz']))); // bool(true)
var_dump(is_countable(new ArrayIterator())); // bool(true)
var_dump(is_countable(new stdClass())); // bool(false)

Example #2: is_countable with conditions

$foo = ['', []];
if (is_countable($foo)) {
    var_dump(count($foo)); // int(2)

Backward Incompatible Changes

None, as this is a new function only.

Proposed PHP Version

The next PHP 7.x, current version 7.3.

RFC Impact

This RFC has no impact on SAPIs, existing extensions, Opcache, etc.

Future Scope

Is out of scope, but a new countable type could be cogitated in the future.

Proposal and Patch

The patch (including tests) for this proposal is available in GitHub Pull Request #3026.



An option needs 50%+1 votes to win

is-countable function (100% approved)
User Vote
aeoris Yes
ashnazg Yes
colinodell Yes
cpriest Yes
danack Yes
diegopires Yes
emir Yes
galvao Yes
guilhermeblanco Yes
hywan Yes
kalle Yes
kelunik Yes
kguest Yes
kinncj Yes
lcobucci Yes
levim Yes
lex Yes
marcio Yes
ocramius Yes
pmjones Yes
pmmaga Yes
pollita Yes
salathe Yes
svpernova09 Yes
yunosh Yes