PHP sizeof() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, sizeof() returns the number of elements in an array. It is an alias of count().

Because it is an alias, sizeof() works exactly the same as count(). Most PHP code uses count(), which is the clearer name. Beginners sometimes expect sizeof() to return the size in bytes like in C, but in PHP it only counts elements.

Syntax

Call sizeof() with the values you need. Start with this line.

PHP
$total = sizeof($items);
CallWhat it does
sizeof($items)Returns the number of elements in $items.
sizeof($nested, COUNT_RECURSIVE)Counts the elements inside nested arrays too.
count($items)The main name for the same function.

sizeof() is an alias of count(). An empty array gives 0. The second argument controls whether nested arrays are counted too.

Library declarations
PHP
sizeof(Countable|array $value, int $mode = COUNT_NORMAL): int

Parameters

ParameterDescription
valueRequired. The array (or Countable object) to count.
modeOptional. COUNT_NORMAL (default) counts the top-level elements. COUNT_RECURSIVE also counts the elements of nested arrays.

Return Value

Return ValueDescription
intThe number of elements in the array.

PHP Examples

Example 1 — Count the Elements of an Array

sizeof() simply returns how many elements an array holds.

PHP
<?php
$items = [1, 2, 3];
$empty = [];

echo sizeof($items), PHP_EOL;
echo sizeof($empty), PHP_EOL;

How It Works

  1. $items has three elements.
  2. sizeof($items) returns 3.
  3. $empty has no elements.
  4. sizeof($empty) returns 0.

Example 2 — Counting Nested Arrays

By default only the top level is counted. COUNT_RECURSIVE also counts what is inside the inner arrays.

PHP
<?php
$nested = [1, [2, 3], [4, 5, 6]];

echo sizeof($nested), PHP_EOL;
echo sizeof($nested, COUNT_RECURSIVE), PHP_EOL;

How It Works

  1. The outer array has three elements: the number 1 and two inner arrays.
  2. The normal count is 3.
  3. COUNT_RECURSIVE counts the 3 top-level elements plus 2 and 3 elements inside the inner arrays.
  4. The total is 3 + 2 + 3, which is 8.

Example 3 — sizeof() Equals count() in a Loop

Both names give the same number. This example also uses the result to control a for loop.

PHP
<?php
$names = ["Asha", "Tom", "Ann"];

var_dump(sizeof($names) === count($names));

for ($i = 0; $i < sizeof($names); $i++) {
    echo $i, ": ", $names[$i], PHP_EOL;
}

How It Works

  1. sizeof() and count() both return 3, so the strict comparison is true.
  2. The for loop starts at 0 and runs while $i is less than the number of elements.
  3. Each time, the loop prints the index and the matching name.
  4. The loop stops when $i reaches 3.

Frequently Asked Questions

It returns the number of elements in an array. It is an alias of count().
Yes. They are identical in behavior. Use count() because it is the more common name.
No. Unlike C, PHP's sizeof() counts elements, not bytes. For the length of a string, use strlen().
Pass COUNT_RECURSIVE as the second argument, like sizeof($array, COUNT_RECURSIVE).
PHP 8 throws a TypeError, because a string is not countable. PHP 7.2 to 7.4 gave a warning and returned 1.

Next: sort()

sort() sorts the values of an array from lowest to highest and gives them new keys 0, 1, 2.

sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful