PHP sizeof() Function
Quick summary: The PHP sizeof() function is an alias of count() and returns the number of elements in an array or Countable object.
PHP sizeof() Syntax
sizeof(Countable|array $value, int $mode = COUNT_NORMAL): int
PHP
PHP sizeof() Basic examples
var_dump(sizeof([1,2,3]));
PHP
Output:
int(3)
Returns the number of elements in the array.
PHP sizeof() Real-world usage
$items = ['a','b'];
if (sizeof($items) > 0) {
echo 'Not empty';
}
PHP
Output:
Not empty
Checks whether a collection contains elements.
PHP sizeof() Edge cases
var_dump(sizeof([]));
PHP
Output:
int(0)
Returns 0 for an empty array.
PHP sizeof() Common mistakes
Assuming sizeof differs from count
sizeof is just an alias of count.
Incorrect
sizeof($arr); // expecting different behavior
Correct
count($arr);
Use count() for clarity and consistency.
PHP sizeof() Frequently Asked Questions
What does sizeof() do in PHP?
sizeof() is an alias of count() and returns number of elements.
Is sizeof() different from count()?
No, they are identical.
Does sizeof() modify array?
No.
Supports COUNT_RECURSIVE?
Yes.
Use case?
Counting elements.
Works with objects?
Yes if Countable.
Performance?
Same as count().
Recommended usage?
Prefer count() for clarity.
Returns 0 for empty array?
Yes.
Handles nested arrays?
Yes.
Can it fail?
On invalid types.
Alternative?
count().