What is PHP sizeof() function?
PHP sizeof() function can tell you the total number of elements exist in an array or in a countable object.
Syntax (For Array):
sizeof(array_name, mode)
Parameters:
The function has two parameters-
array_name (Required): The name of the array whose element you want to count of (Check example 1).
mode (Optional): it specifies the mode of the array. Which means, whether you want to count elements of the inner arrays if it is a multidimensional array. It has two values-
- COUNT_NORMAL or 0: Use one of two values if you don’t want to count the inner arrays’ elements. This is the default value too. That means, even if you don’t mention the second parameter of the sizeof() function, it assumes the 2nd parameter as COUNT_NORMAL or 0. (Check example 2).
- COUNT_RECURSIVE or 1: If the specified array is a multidimensional array and you want to count elements of the inner arrays, use either one of two values (Check example 3)
Syntax (For countable object):
sizeof(Countable obj)
Parameters:
It has only one parameter-
Countable_obj: The countable object name whose element you want to count of. (Check example 5).
Check below what the function returns for an array or a countable object in PHP version 8-
Array or Object – | For Array – | For Countable Object – |
Array with element or an object that implements the Countable interface with element. | It returns element number (Check example 1) | It returns element number (Check example 5) |
Empty array or empty object. | It returns 0 (Check example 4) | It returns 0 (Check example 6) |
- If it is neither an array nor an object that implements the Countable interface, it returns TypeError (Check example 7 & Check example 8)
Examples:
Example 1: Getting number of element of an array –
<?php $numbers = [1, 2, 3, 4, 5]; echo sizeof($numbers); ?>
Explanation:
The array $numbers has 5 elements, so, sizeof() function displays its element as 5 (line 3).
Example 2: Getting number of element of a multidimensional array –
<?php $number = [1, 2, 3, [4, 5], 6]; echo sizeof($numbers); ?>
Explanation:
As there is no second parameter in the sizeof() method here, it becomes COUNT_NORMAL or 0 by default and it will not count the elements of the inner arrays. So, it returns outer array’s element number, 5.
Example 3: Getting number of element of a multidimensional array that uses COUNT_RECURSIVE-
<?php $number = [1, 2, 3, [4, 5], 6]; echo sizeof($number, COUNT_RECURSIVE); // Output: 7 ?>
Explanation:
The COUNT_RESURSIVE parameter is in the second parameter in the sizeof() function here, it counts the elements inside the inner arrays . So, it returns the element number, 7.
Example 4: Getting number of elements of an empty array-
<?php $numberEmpty = []; echo sizeof($numberEmpty); ?>
Explanation:
As the array contains no element, so the sizeof() function returns 0.
Example 5: Getting number of element of an object that doesn’t implements the Countable interface-
<?php class GetMethodNumber implements Countable{ // Methods public function oneMethod(){} public function twoMethod(){} public function count(): int{ return count(get_class_methods($this)); } } $countable_obj = new GetMethodNumber(); echo sizeof($countable_obj); ?>
Explanation:
The class GetMethodNumber implement the Countable interface, so, the sizeof() returns its element number which is 3 here.
Example 6: Getting number of element of an object that doesn’t contain any countable objects-
<?php class GetMethodNumber3 implements Countable{ // Properties public function count(): int{ return count(get_class_vars(get_class($this))); } } $countable_obj = new GetMethodNumber3(); echo sizeof($countable_obj); ?>
Explanation:
The class GetMethodNumber3 contains any element, so, the sizeof() function returns 0.
Example 7: Getting number of element a non-array value –
<?php $newVariable = ""; echo sizeof($newVariable); ?>
Explanation:
The $newVariable is not an array, so the sizeof() function returns TypeError.
Example 8: Getting number of element an object that doesn’t implement the Countable interfaces –
<?php class GetMethodNumber2{ // Methods public function oneMethod(){} public function twoMethod(){} public function count(): int{ return count(get_class_methods($this)); } } $countable_obj = new GetMethodNumber2(); echo sizeof($countable_obj); ?>
Explanation:
As the object doesn’t implement the Countable interfaces (though the object contains elements), the sizeof() function returns TypeError.
Practical Usages:
PHP sizeof() function is a very useful function. This function can be used in the following situations-
- To check whether an array is empty or not.
- To check the length of an array.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP sizeof() Function
The sizeof() function is very useful when you want to count element number of an array or countable object number of an object. This is one of the many built-in PHP array functions. it is a alias of count() function.
Reference:
https://www.php.net/manual/en/function.sizeof.php
https://www.php.net/manual/en/countable.count.php