What is PHP count() function?
PHP count() function can tell you the total number of element exist in an array or in a countable object.
Syntax (For Array):
count(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 these values, in case you don’t want to count the inner arrays’ element. This is the default value. So, even if you don’t mention the second parameter of the count() function, it will assume 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 the values (Check example 3)
Syntax (For countable object):
count(Countable obj)
Parameters:
It has only one parameter-
Countable_obj: The countable object name whose element you want to count of. (Check example 5).
Return Values:
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 count($numbers); // Output: 5
?>
Explanation:
The array $numbers has 5 elements, so, count 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 count($number); // Output: 5
echo count($number, COUNT_RECURSIVE); // Output: 7
?>
Explanation:
As there is no second parameter in the count() method in line 3, 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. In line 4, the second parameter is set to COUNT_RECURSIVE which calculates the inner both inner and outer arrays’ element and returns 7.
Example 3: Getting number of element of a multidimensional array that uses COUNT_RECURSIVE-
<?php
// Object with elements that implements the Countable interface
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 count($countable_obj); // Output: 3
?>
Explanation:
When you try to count an object (line 13), the method Countable::count() is executed (line 8) which means it counts elements of the object. Here, the class GetPropertyNumber has 3 methods and the method get_class_methods() returns the class methods’ names as an array and the built-in count method (line 9) returns this array’s element (here 3).
Example 4: Getting number of elements of an empty array-
<?php
$numberEmpty = [];
echo count($numberEmpty); // Output:0
?>
Explanation:
As the array contains no element, so the count() 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 count($countable_obj); // Output: 3
?>
Explanation:
The class GetMethodNumber implement the Countable interface, so, the count() 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{
public function count(): int{
return count(get_class_vars(get_class($this)));
}
}
$countable_obj = new GetMethodNumber3();
echo count($countable_obj); // 0
?>
Explanation:
The class GetMethodNumber3 contains any element, so, the count() function returns 0.
Example 7: Getting number of element a non-array value –
<?php
$newVariable = "";
echo count($newVariable); // TypeError
?>
Explanation:
The $newVariable is not an array, so the count() 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 count($countable_obj); // TypeError
?>
Explanation:
As the object doesn’t implement the Countable interfaces (though the object contains 2 elements), the count function returns TypeError.
Practical Usages:
PHP count() function is a very useful function and one of the frequently used functions. This function is often 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 count() Function
count() is one of the most useful PHP array function. it is a alias of PHP sizeof() function.
Reference:
https://www.php.net/manual/en/function.count.php
https://www.php.net/manual/en/countable.count.php