Take the State of PHP 2026 Survey →

is_countable function View as Markdown

Implemented (PHP 7.3)
Authors
Gabriel Caruso (carusogabriel34@gmail.com)
Date
2018-01-21
Version
1.0
Source
wiki.php.net/rfc/is-countable

Vote

is-countable function closed
Yes
25
No
0
25 voters
aeoris Yes 2018-02-28 06:09
ashnazg Yes 2018-02-28 14:34
colinodell Yes 2018-02-28 14:29
cpriest Yes 2018-02-27 03:09
danack Yes 2018-02-27 01:52
diegopires Yes 2018-02-26 17:15
emir Yes 2018-03-02 09:31
galvao Yes 2018-02-28 15:16
guilhermeblanco Yes 2018-02-26 19:39
hywan Yes 2018-03-01 09:53
kalle Yes 2018-02-26 17:33
kelunik Yes 2018-02-26 17:34
kguest Yes 2018-02-27 10:34
kinncj Yes 2018-02-26 19:16
lcobucci Yes 2018-02-27 08:07
levim Yes 2018-02-28 03:32
lex Yes 2018-02-27 19:40
marcio Yes 2018-02-27 02:49
ocramius Yes 2018-02-26 17:21
pmjones Yes 2018-02-28 02:01
pmmaga Yes 2018-02-26 18:29
pollita Yes 2018-03-01 15:04
salathe Yes 2018-03-02 09:44
svpernova09 Yes 2018-02-28 12:48
yunosh Yes 2018-03-02 09:13

Discussions on the mailing list

[RFC] is_countable
by Gabriel Caruso · 7 messages
[VOTE] is_countable
by Gabriel Caruso · 1 message

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

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);
}

Proposal

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.

Before:

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

After:

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

Documentation

Description

bool is_countable(mixed $var)

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

Parameters

var

The value to check

Return Values

Returns TRUE if var is countable, FALSE otherwise

Examples

Example #1: is_countable

<?php
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

<?php
$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.

Proposed Voting Choices

Since this is not a PHP language changed, a 50% + 1 majority is required.

Voting begins 2018-02-26 17:00 UTC and ends 2018-03-02 17:00 UTC.

Vote: is-countable function — Yes: 25 · No: 0 (closed)

Proposal and Patch

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

References

https://wiki.php.net/rfc/counting_non_countables https://externals.io/message/101648