sizeof()
Learn how the PHP sizeof() function counts array elements, how it relates to count(), and how COUNT_RECURSIVE handles nested arrays, with runnable examples.
Introduction
PHP ships with many built-in functions for working with arrays. sizeof() is one of the simplest: it returns the number of elements an array holds. This page explains how sizeof() works, how it behaves with nested (multidimensional) arrays, and why it is really just another name for count().
What is sizeof()?
sizeof() returns the number of items in an array. It is an alias for count() — the two functions are the same internally, so they share identical behavior, return values, and performance. Anything you can do with count() you can do with sizeof(), and vice versa.
Because it is an alias and not a separate implementation, there is no situation where one is faster or more capable than the other.
Syntax
sizeof(array|Countable $value, int $mode = COUNT_NORMAL): int| Parameter | Required | Description |
|---|---|---|
$value | Yes | The array (or Countable object) whose elements you want to count. |
$mode | No | How to count. COUNT_NORMAL (default) counts only top-level elements; COUNT_RECURSIVE also counts elements inside nested arrays. |
It returns an int — the number of elements.
Basic example
Counting the elements of a flat array:
Output:
3The array contains three elements, so sizeof() returns 3.
Counting nested arrays with COUNT_RECURSIVE
By default sizeof() only counts the elements at the top level. To also count the elements inside sub-arrays, pass COUNT_RECURSIVE as the second argument:
<?php
$data = [
'fruits' => ['apple', 'banana'],
'vegetables' => ['carrot'],
];
echo sizeof($data); // top-level keys only
echo "\n";
echo sizeof($data, COUNT_RECURSIVE); // top-level + nested itemsOutput:
2
5COUNT_NORMAL sees two top-level keys (fruits and vegetables). COUNT_RECURSIVE counts those two keys plus the three values inside them (apple, banana, carrot), giving 5.
sizeof() vs count()
Use whichever name reads better to you — the result is always the same:
<?php
$nums = [10, 20, 30, 40];
var_dump(sizeof($nums) === count($nums));Output:
bool(true)In practice the official PHP documentation recommends count(), since the name sizeof is easy to confuse with C's sizeof operator (which measures memory size, not element count). Reaching for count() keeps your code consistent with most modern PHP codebases.
Common gotchas
- It does not count characters in a string.
sizeof('hello')triggers a warning and returns1, not5. Usestrlen()for string length. - It does not sum sizes of multiple arrays.
sizeof()takes one array argument, not several. - An empty array returns
0. That makes it safe to use directly in a loop condition such asfor ($i = 0; $i < sizeof($arr); $i++).
Conclusion
sizeof() returns the number of elements in an array and is a direct alias of count(). Pass COUNT_RECURSIVE when you need to count nested elements as well. For new code, prefer count() for clarity — but both behave identically.