count($arr) returns the number of elements in an array: count(['red', 'green', 'blue']) is 3. PHP arrays have no length property, so count() is how you get an array's length or size.
count() is fast: PHP stores the size inside the array, so counting a million elements costs the same as counting three.
Count a multidimensional array
By default count() counts only the top level. Pass COUNT_RECURSIVE (or 1) as the second argument to count every element at every depth. The nested arrays themselves are counted too, which is why the total is often larger than the number of leaf values.
Both shortcuts assume exactly two levels. For deeper nesting in a multidimensional array, count the leaves with array_walk_recursive(): $n = 0; array_walk_recursive($arr, function () use (&$n) { $n++; });.
count vs sizeof
sizeof() is an alias of count(): same arguments, same result. Use count(). It is what almost all PHP code and documentation uses, and sizeof suggests a size in bytes to anyone who knows C.
Count values that match a condition
count() has no condition argument. Filter first, then count: array_filter() keeps the elements your callback accepts. To count how many times each value appears, use array_count_values().
Count characters in a string
count() is for arrays. For a string, use strlen() for bytes or mb_strlen() for characters; they differ as soon as the text has accents or Japanese. The strlen page covers the difference. To count words, split first.
str_word_count() treats non-ASCII letters as separators, so naïve counts as two words. Splitting on spaces is more predictable for real text.
count() on null or a string throws
Since PHP 8, count() accepts only arrays and objects that implement Countable. Anything else throws a TypeError, where PHP 7.2 to 7.4 returned 0 (for null) or 1 (for a string or number) with a warning. The usual cause is a variable that is sometimes null, such as a missing key or a function that returned nothing.
Default the value to an empty array with ??, and the count is 0:
Check if an array is empty
To ask "does this array have anything in it", compare with an empty array or use empty(). count($arr) === 0 works too, but only when $arr is certainly an array.
empty($x) never throws, even when $x is undefined or null, which makes it the safer check for a value that might not be an array. Its other rules (an empty string and "0" are empty too) are on the empty page.
Frequently Asked Questions
How do I get the length of an array in PHP?
Use count($arr). It returns the number of elements as an integer, so count([10, 20, 30]) is 3. There is no length property on PHP arrays.
What is the difference between count and sizeof in PHP?
None. sizeof() is an alias of count() and takes the same arguments. Most code uses count(), and sizeof() can mislead readers who know C, where it means bytes.
How do I count the elements of a multidimensional array?
count($arr) counts only the top level. count($arr, COUNT_RECURSIVE) counts every element at every depth, and the nested arrays count as elements too.
Why does count() throw a TypeError in PHP 8?
Since PHP 8.0, count() accepts only arrays and objects that implement Countable. count(null) throws TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given, and count('abc') the same with string given. Use count($x ?? []) for a value that may be null, and strlen() for strings.