Open In App

PHP count() Function

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The count() function in PHP is used to count the number of elements in an array or the countable properties of an object. The function returns an integer value representing the number of items present.

Syntax:

count($array, mode)

In this syntax:

  • $array (mandatory): Refers to the array, whose elements are needed to be counted.
  • mode (optional): This is used to set the mode of the function. The parameter can take two possible values, either 0 or 1. 1 generally indicates to count the values of the array recursively. This helps in counting the multidimensional array. The default value is 0 or False.
  • Return Values: The function returns the number of elements in an array.

Now, let us understand with the help of the example:

PHP
<?php
   $a[0] = 10;
   $a[1] = 25;
   $a[2] = 75;
   $a[3] = 100;
   $a[4] = 125;
   
   $ans = count($a);
   print($ans);
?> 

Output
5 

Counting Non-Countable Data Types

If you try to use count() on a non-countable data type (like a string or integer), it will return 0.

Now, let us understand with the help of the example:

PHP
<?php
$string = "Hello World!";
echo count($string);  

$number = 100;
echo count($number);  
?>

Output
Warning: count(): Parameter must be an array or an object that implements Countable in /home/guest/sandbox/Solution.php on line 3
1
Warning: count(): Parameter must be an array or an object that impl...

Using count() with Objects

PHP objects that implement the Countable interface can also be counted. If an object implements the Countable interface, calling count() will return the number of elements defined by the object’s count() method.

Now, let us understand with the help of the example:

PHP
<?php
class FruitBasket implements Countable {
    private $fruits = array("apple", "banana", "orange");
    
    public function count() {
        return count($this->fruits);
    }
}

$basket = new FruitBasket();
echo count($basket);  
?>

Output
3

Recursive Count()

When working with multidimensional arrays, sometimes you want to count not only the elements at the top level but also the elements inside nested arrays. In this case, you can use the COUNT_RECURSIVE with the count() function to count all elements recursively, including those in nested arrays.

Now, let us understand with the help of the example:

PHP
<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
              'cars' => array('honda', 'thar', 'skoda', 'hyundai'));

echo "Recursive count: " . count($food, COUNT_RECURSIVE) . "\n";
echo "Normal count: " . count($food) . "\n";
?>

Output
Recursive count: 9
Normal count: 2

Performance Considerations

While count() is generally fast, there are some considerations when using it:

  • Recursive Counting: If you are using COUNT_RECURSIVE with large multidimensional arrays, it can become slower because PHP needs to traverse all levels of the array.
  • Countable Objects: When using count() with objects, ensure that the object implements the Countable interface to avoid errors.

Practical Use Cases for count()

The count() function is often used in scenarios such as:

  • Looping through arrays: Determining the number of iterations needed.
  • Validating data: Checking if an array is empty or contains items.
  • Pagination: Counting the number of records in a dataset to display pagination links.

Conclusion

The count() function is an essential tool in PHP for counting elements in arrays and objects. It simplifies many common tasks such as checking the size of arrays, validating input data, or implementing pagination.



Next Article

Similar Reads