I have a var: $a
. I don\'t know what it is. I want to check if I can count it. Usually, with only array, I can do this:
if (is_array($a)) {
According to the documentation, You can use is_countable
function:
if (is_countable($a)) {
echo count($a);
}
For previous PHP versions, you can use this
if (is_array($foo) || $foo instanceof Countable) {
return count($foo);
}
or you could also implement a sort of polyfill for that like this
if (!function_exists('is_countable')) {
function is_countable($c) {
return is_array($c) || $c instanceof Countable;
}
}
Note that this polyfill isn't something that I came up with, but rather pulled directly from the RFC for the new function proposal https://wiki.php.net/rfc/is-countable