PHP count() Function

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  // EXAMPLE 1
$numbers = [1, 2, 3, 4, 5];
echo count($numbers);
echo "<br>";
$numberEmpty = [];
echo count($numberEmpty);
?>

Explanation:

The array $numbers has 5 elements, so, count function displays its element as 5 (line 3). The $numberEmpty is an empty array, it has no element, so, the count function display its element number 0 (line 6). As $new_array has not been declared as an array so, the count function displays TypeError.

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 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. In line 5, 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 an object that implements the Countable interface –

<?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 element of an object that doesn’t implements the Countable interface-

<?php
// Object that doesn't implements the Countable interface
class GetMethodNumber{
  // 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: TypeError
?>

Explanation:

The class GetMethodNumber doesn’t implement the Countable interface, but, the count() function is trying to counting its element numbers, so, it 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.

Reference:

https://www.php.net/manual/en/function.count.php
https://www.php.net/manual/en/countable.count.php