sizeof

Counts the elements in an array or Countable object; an alias of count().

Signature
sizeof(Countable|array $value, int $mode = COUNT_NORMAL): int
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valueCountable|arrayAn array, or an object implementing the Countable interface.
$modeoptionalint= COUNT_NORMALCOUNT_NORMAL counts only the top level. COUNT_RECURSIVE (or 1) also counts the elements of nested arrays.

Return value

intReturns the number of elements in the array, or the value returned by the object's count() method for a Countable object.

Errors

Throws a TypeError if value is not an array and does not implement Countable — including null, strings and plain objects.

Changelog

  • PHP 8.0.0Passing a non-countable value now throws a TypeError instead of emitting a warning.
  • PHP 7.2.0Passing a non-countable value started emitting a warning; before 7.2 it silently returned 1 (or 0 for null).

sizeof() is nothing more than a second name for count(). Same engine function, same parameters, same result, same speed — there is no behavioural difference whatsoever between the two.

It returns the number of elements in an array, or the value of count() on an object implementing Countable such as ArrayObject or a collection class. Pass COUNT_RECURSIVE as the second argument to include nested arrays; the outer elements still count, so [[1,2],[3,4,5]] gives 7, not 5.

Should you use it?

Prefer count() in new code. Developers arriving from C or C++ read sizeof as "number of bytes", which it never means in PHP, and most coding standards and linters flag the alias. It survives purely for backwards compatibility with code written in the PHP 4 era.

Things that catch people out

  • Since PHP 8.0 anything that is not an array or Countable throws a TypeError — including null, which used to return 0, and a plain stdClass, which used to return 1.
  • It is not a string length function. Use strlen() for bytes or mb_strlen() for characters.
  • Calling it inside a for condition re-counts on every iteration; hoist it into a variable for large arrays.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Reading or maintaining legacy PHP 4/5 code where sizeof() was the house style.
  • Counting rows returned from a query before deciding whether to render a table or an empty state.
  • Counting a nested structure with COUNT_RECURSIVE to size a flattened export.

Frequently asked questions

What is the difference between sizeof() and count() in PHP?

There is none — sizeof() is a registered alias of count(), so they compile to the same call with identical behaviour and performance. count() is the recommended spelling.

Does sizeof() return the memory size of a variable?

No. Unlike C's sizeof operator it returns the number of elements, never bytes. For memory use memory_get_usage(), and for string length use strlen() or mb_strlen().

Why does sizeof() throw a TypeError?

Because you passed something that is neither an array nor a Countable object — commonly null from a function that found nothing, or a string. Since PHP 8.0 that is a TypeError instead of a warning; guard with is_countable() or coalesce to [] first.

More examples

Countable objects, and what is not countable

Prints 3 for the ArrayObject, because it implements Countable. The plain stdClass throws a TypeError instead — on PHP 5 the same call would have quietly returned 1, which is exactly the kind of silent wrong answer PHP 8 turned into an error.

Example · php
<?php
$obj = new ArrayObject(['a', 'b', 'c']);
echo sizeof($obj), "\n";

$plain = new stdClass();
$plain->a = 1;
try {
    echo sizeof($plain);
} catch (TypeError $e) {
    echo 'TypeError: ', $e->getMessage(), "\n";
}

COUNT_RECURSIVE counts containers too

Prints 2, then 7, then 5. The recursive count is 7 because it adds the two container arrays to the five leaf values. If you want just the leaves, flatten first and count that — the third line shows the difference.

Example · php
<?php
$tree = [
    'fruit' => ['apple', 'pear'],
    'veg'   => ['leek', 'kale', 'peas'],
];

echo sizeof($tree), "\n";
echo sizeof($tree, COUNT_RECURSIVE), "\n";

$flat = array_merge(...array_values($tree));
echo sizeof($flat), "\n";

Checking for empty

The three checks all print bool(true) for an empty array. For a simple emptiness test $rows === [] is clearer and avoids walking a Countable object's count() method; reserve sizeof()/count() for when you actually need the number.

Example · php
<?php
$rows = [];

var_dump(sizeof($rows) === 0);
var_dump($rows === []);
var_dump(empty($rows));

$rows[] = 'first';
echo sizeof($rows), "\n";

Discussion

  • Be the first to comment on this lesson.