W3docs

count()

In PHP, the count() function is a built-in array function that is used to return the number of elements in an array or the number of properties in an object.

The count() function is a built-in PHP function that returns the number of elements in an array or the number of properties in an object. It works with arrays, objects, and some other types of data.

Syntax of the count() Function

The syntax of the count() function is straightforward. It takes one argument, which is the array or object you want to count. The function returns an integer representing the number of elements or properties.

<?php

count(mixed $array_or_object, int $mode = COUNT_NORMAL): int

Note: The typed signature above reflects PHP 8.0+. In PHP 8.0+, passing a non-countable type (such as a string or integer) throws a ValueError. Older PHP versions lack strict type hints but function identically. Note: sizeof() is a legacy alias for count() and behaves identically.

The second argument, $mode, is optional and is used to specify how the count() function should behave. It accepts two possible values:

  • COUNT_NORMAL: This is the default mode and behaves the same way as the count() function without the $mode parameter. It simply counts the number of elements in the array or properties in the object.
  • COUNT_RECURSIVE: This mode recursively counts the elements in nested arrays and objects.
<?php

$data = array('apple', array('banana', 'cherry'));

echo count($data);           // Output: 2
echo count($data, COUNT_RECURSIVE); // Output: 4

Example Usage of the count() Function

Let's take a look at some practical examples of how the count() function can be used to count the elements in an array and the properties in an object.

Example 1: Counting the Elements in an Array

Counting the Elements in an Array in PHP

<?php

$fruits = array('apple', 'banana', 'cherry');

echo count($fruits); // Output: 3

In this example, we have an array called $fruits containing three elements. The count() function returns 3, representing the total number of items in the array.

Example 2: Counting the Properties in an Object

Counting the Properties in an Object in PHP

<?php

class Person implements Countable
{
    public $name;
    public $age;

    public function count()
    {
        return count(get_object_vars($this));
    }
}

$person = new Person();
$person->name = 'John';
$person->age = 30;

echo count($person); // Output: 2

In this example, we have a simple class called Person with two public properties, $name and $age. We create a new instance of the Person class and set the values of its properties. Because the class implements the Countable interface, calling count($person) invokes the custom count() method, which returns 2. This demonstrates how count() behavior can be customized for objects.

Conclusion

The count() function is a simple but powerful function in PHP that can be used to count the elements in an array or the properties in an object. By using this function, you can easily determine the number of elements in your data structures and use this information to write more efficient and effective code. As experienced PHP programmers, we recommend using the count() function as part of your data manipulation strategy to ensure that your code is scalable and performant.

Practice

Practice

What is the functionality of the 'count()' function in PHP?