PHP array length: counting with count() and sizeof()
PHP array length is the element count of an array, and count() is the standard way to get it. To count PHP array elements, call count($array) (or sizeof($array), which is the same function). count() returns the number of top-level elements for both indexed and associative arrays. For nested structures, COUNT_RECURSIVE counts sub-arrays too, so use it only when that behavior matches what you want.
PHP Array Length Example For Counting Elements
Output:
Output will appear here...
Output:
3
How This Example Works
$colorsholds an indexed array with three string elements.count($colors)returns3— the total number of top-level elements. This works the same for associative arrays:count()counts keys, not values.- An empty array returns
0, socount()doubles as an emptiness check when compared against zero.
How To Get Array Length in PHP
PHP has no .length property on arrays. The count() function is the standard way to get the length of a PHP array, and it also works on objects that implement Countable. It always returns an int. In PHP 8.0+, calling count() on a non-countable value throws a TypeError, so guard with is_countable() when the value might not be an array.
Common Mistakes With PHP count()
Using strlen() instead of count()
Wrong:
$items = ["a", "b", "c"];
echo strlen($items); // TypeError in PHP 8+
Right:
echo count($items); // 3
strlen() measures the byte length of a string, not the number of array elements.
Calling count() on non-countable values
Wrong:
$maybeArray = "abc";
echo count($maybeArray); // TypeError in PHP 8+
Right:
$length = is_countable($maybeArray) ? count($maybeArray) : 0;
echo $length;
If a value might be a scalar or null, check is_countable() before counting.
count() vs sizeof() in PHP
count() | sizeof() |
|---|---|
| Canonical function documented in the PHP manual. | Alias of count() — identical behavior and performance. |
| Universally recognized across PHP codebases. | Can confuse developers from C/C++ where sizeof returns byte size. |
Prefer count() in all new code. sizeof() exists for compatibility but adds no value and can mislead readers familiar with other languages.
COUNT_NORMAL vs COUNT_RECURSIVE
$groups = ["fruits" => ["apple", "pear"], "colors" => ["red"]];
echo count($groups); // 2 (top-level keys)
echo count($groups, COUNT_RECURSIVE); // 5 (2 keys + 3 values)
COUNT_NORMAL (the default, value 0) counts only top-level elements. COUNT_RECURSIVE (value 1) walks all nested arrays and adds their elements to the total. Arrays with circular references trigger a warning during recursive counting.
Use COUNT_RECURSIVE only when counting sub-arrays as elements makes sense for your use case — it does not return a leaf-value count.
FAQ
How do you get array length in PHP?
Call count($array) to get the number of elements. This works with indexed arrays, associative arrays, and objects that implement Countable. An empty array returns 0.
Does count() work on associative arrays?
Yes. count() counts keys regardless of whether they are numeric or string-based. count(["a" => 1, "b" => 2]) returns 2.
What happens when you pass a non-array to count() in PHP 8?
PHP 8.0+ throws a TypeError for non-countable values like strings, integers, null, or plain objects. In PHP 7.2+, it emitted a warning and returned 1 for scalars (and 0 for null), which could mask bugs.