is_countable
is_countable(mixed $value): bool
Checks whether a value can be counted with count() (array or Countable, PHP 7.3+). Contrasts the everything-has-length camp (len()/length asks the object directly) with the explicit-interface camp (count requires Countable).
C C23
#include <stdio.h>
int main(void) {
/* C asks the TYPE, not the object: array length is computed from
the size formula - decayed pointers forget it entirely */
int arr[] = {1, 2, 3};
printf("%zu\n", sizeof arr / sizeof arr[0]); /* 3: compile-time math */
int *p = arr; /* array decays to pointer */
(void) p;
/* sizeof p / sizeof p[0] would be garbage: the length lives in the type */
return 0;
}
3
C++ C++26
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2};
std::cout << v.size() << "\n"; // 2: the container owns its size
int arr[] = {1, 2, 3};
std::cout << sizeof arr / sizeof arr[0] << "\n"; // 3: raw arrays still ask the type
// std::size(arr) wraps the same math in the standard library
std::cout << std::size(arr) << "\n";
return 0;
}
2 3 3
C# v11.0.100
var list = new List<int> { 1, 2 };
Console.WriteLine(list.Count); // 2: collections expose a Count property
Console.WriteLine("abc".Length); // 3: strings use Length instead
// LINQ Count() counts ANY IEnumerable but may walk the whole sequence -
// a lazy source gets enumerated just to be measured
Console.WriteLine(new[] { 1, 2, 3 }.Count());
2 3 3
Go v1.27.4
package main
import "fmt"
func main() {
// len applies to a closed type set: string, array, slice, map, channel
fmt.Println(len([]int{1, 2})) // 2
fmt.Println(len("abc")) // 3: bytes, not characters
fmt.Println(len(map[string]int{"a": 1})) // 1
// no user-defined Countable interface exists: your own types
// expose Len() as an ordinary method, checked only by convention
}
2 3 1
Java v27.0.1
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = List.of(1, 2);
System.out.println(list.size()); // 2: collections use size()
System.out.println("abc".length()); // 3: strings use length()
// the question lives on interfaces: Collection#size exists only
// on the interface - there is no universal len(everything)
}
}
2 3
JavaScript v26.13.1
console.log([1, 2].length); // 2: the everything-has-length camp
console.log("abc".length); // 3
console.log(({}).length); // undefined: just a missing property, no error
// length is an ordinary property - it says nothing about countability,
// and a function's length even counts its declared parameters
console.log(((a, b) => 0).length); // 2
2 3 undefined 2
PHP v8.6.0
// is_countable: the runtime gate for count() (PHP 7.3+)
var_dump(is_countable([1, 2])); // arrays: yes
var_dump(is_countable(new ArrayObject([1]))); // Countable: yes
var_dump(is_countable('string')); // no
var_dump(is_countable(42)); // no
class Bag implements Countable {
private array $items = [1, 2, 3];
public function count(): int { return count($this->items); }
}
var_dump(count(new Bag())); // 3: count delegates to count()
var_dump(is_countable(new Bag()));
bool(true) bool(true) bool(false) bool(false) int(3) bool(true)
Python v3.15.2
print(len([1, 2])) # 2: len asks the object's __len__ - duck typing
print(len("abc")) # 3
print(len({"a": 1})) # 1: dicts count keys
try:
print(len(42))
except TypeError as e:
print(e) # object of type 'int' has no len()
2 3 1 object of type 'int' has no len()
Rust v1.99.1
fn main() {
let v = vec![1, 2];
println!("{}", v.len()); // len is a slice method, not a universal protocol
// iterators may not know their length: size_hint gives (lower, upper)
let it = v.iter();
println!("{:?}", it.size_hint()); // (2, Some(2)): exact here, unknown elsewhere
}
2 (2, Some(2))
TypeScript v26.13.1
console.log([1, 2].length); // 2: the everything-has-length camp
console.log("abc".length); // 3
console.log(({} as Record<string, number>).length); // undefined: no error
console.log(((a: string, b: string) => 0).length); // 2: declared parameters
2 3 undefined 2
Language differences
C: the TYPE is asked, not the object - array length is compile-time arithmetic (a sizeof division), and once the array decays to a pointer the length is forgotten; C also has the size_t unsigned trap (negatives become huge numbers).
C++: containers own their length via size(); raw arrays still need the sizeof formula or std::size; in iterator-land the counterpart is std::distance (O(n) or O(1) depending on the iterator category).
C#: the explicit-property camp - collections have Count, strings have Length (different names per type); LINQ's Count() counts any IEnumerable but may fully enumerate lazy sources (use with care on hot paths).
Go: len applies to a closed type set (string/array/slice/map/channel) and does not accept custom types - countability is a language builtin, not an interface; string length is a byte count (linking back to the UTF-8 teaching points).
Java: the explicit-interface camp - Collection#size() and String#length() go their own ways with no universal function; arrays use the .length field (not a method). PHP's count() on a non-countable object only warned before 7.2 and throws TypeError from 7.2 - which is exactly why is_countable (7.3+) was born.
JavaScript: the everything-has-length camp with the weakest semantics - length is an ordinary property; plain objects just return undefined without error, and a function's length counts its parameters, a fully drifted meaning.
PHP: the explicit-interface camp - arrays are countable by nature, while objects must implement the Countable interface to be count(); is_countable tests exactly that (an array or a Countable instance).
Python: the everything-has-length camp - len() is a builtin that asks the object's __len__ dunder (duck typing); non-countable objects raise TypeError rather than returning a fake value.
Rust: len() is an inherent method of slices/containers, not a protocol; iterators may not know their length - size_hint returns a (lower, upper) interval, and a lazy sequence only reveals its length once fully consumed.
TypeScript: same as JavaScript; at the type level tuples are also countable (tuple["length"] is a literal type).