count
count(Countable|array $value, int $mode = COUNT_NORMAL): int
Counts the number of elements in an array.
C C23
C23
#include <stdio.h>
int main(void) {
int xs[] = {1, 2, 3};
printf("%zu\n", sizeof xs / sizeof xs[0]); // array length known at compile time
// nested: PHP COUNT_RECURSIVE counts leaves too — walk them manually here
int leaves[] = {1, 2, 3}; // [1, [2, 3]] flattened by hand
printf("%zu\n", sizeof leaves / sizeof leaves[0]);
return 0;
}
Output
3 3
C++ C++26
C++26
#include <iostream>
#include <vector>
int main() {
std::vector<int> xs = {1, 2, 3};
std::cout << xs.size() << "\n";
// nested: PHP COUNT_RECURSIVE counts inner elements too (1 + 2 = 3)
std::vector<std::vector<int>> nested = {{1}, {2, 3}};
int total = 0;
for (const auto &inner : nested) total += (int)inner.size();
std::cout << total << "\n";
return 0;
}
Output
3 3
C# v11.0.100
v11.0.100
var xs = new List<int> { 1, 2, 3 };
Console.WriteLine(xs.Count);
// nested: PHP COUNT_RECURSIVE counts inner elements too (1 + 2 = 3)
var nested = new List<List<int>> { new() { 1 }, new() { 2, 3 } };
Console.WriteLine(nested.Sum(inner => inner.Count));
Output
3 3
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
fmt.Println(len([]int{1, 2, 3}))
// nested: PHP COUNT_RECURSIVE counts inner elements too (1 + 2 = 3)
total := 0
for _, inner := range [][]int{{1}, {2, 3}} {
total += len(inner)
}
fmt.Println(total)
}
Output
3 3
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println(new int[]{1, 2, 3}.length);
// nested: PHP COUNT_RECURSIVE counts inner elements too (1 + 2 = 3)
int total = 0;
for (int[] inner : new int[][]{{1}, {2, 3}}) total += inner.length;
System.out.println(total);
}
}
Output
3 3
JavaScript v26.13.1
v26.13.1
console.log([1, 2, 3].length)
// nested: flat(Infinity) is the JS route to COUNT_RECURSIVE leaf semantics (1 + 2 = 3)
console.log([1, [2, 3]].flat(Infinity).length)
Output
3 3
PHP v8.6.0
v8.6.0
echo count([1, 2, 3]), "\n";
// COUNT_RECURSIVE counts nested containers themselves too: 1 + 2 + 1 = 4
// (only leaves would be 3 — count([1, [2, 3]]))
echo count([1, [2, 3]], COUNT_RECURSIVE);
Output
3 4
Python v3.15.2
v3.15.2
print(len([1, 2, 3]))
# nested: len sees one inner list at top level; count leaves by hand (1 + 2 = 3)
data = [1, [2, 3]]
print(len(data), 1 + len(data[1]))
Output
3 2 3
Rust v1.99.1
v1.99.1
fn main() {
println!("{}", vec![1, 2, 3].len());
// nested: PHP COUNT_RECURSIVE counts inner elements too (1 + 2 = 3)
let nested = vec![vec![1], vec![2, 3]];
let total: usize = nested.iter().map(|v| v.len()).sum();
println!("{}", total);
}
Output
3 3
TypeScript v26.13.1
v26.13.1
const xs: number[] = [1, 2, 3];
console.log(xs.length);
// nested: flat(Infinity) is the TS route to COUNT_RECURSIVE leaf semantics (1 + 2 = 3)
console.log([1, [2, 3]].flat(Infinity).length);
Output
3 3
Language differences
C: computes the array length at compile time with sizeof.
Go: uses len.
Java: .length on arrays and .size() on collections.
JavaScript: the .length property.
PHP: uses count.
Python: uses len.
Rust: uses len.