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. It can also count an object if that object implements the Countable interface. It is one of the most frequently used array functions in PHP, typically to find out how many items a list holds before looping over it, paginating it, or validating it.

This chapter covers the syntax, the optional recursive counting mode, how count() behaves on objects and edge-case values, and the most common mistakes to avoid.

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

With COUNT_NORMAL (the default) count($data) returns 2: the outer array holds two elements — the string 'apple' and the inner array. With COUNT_RECURSIVE, PHP also counts the elements inside every nested array, so it returns 4: 'apple', the inner array itself, 'banana', and 'cherry'. Use the recursive mode only when you genuinely need the total count across all nesting levels — it walks the entire structure and is slower on large, deeply nested arrays.

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— editable, runs on the server

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.

Note: Without the Countable interface, passing an ordinary object to count() raises a TypeError in PHP 8.0+. count() does not count an object's properties automatically — if you want that, use count(get_object_vars($obj)) directly.

Common Mistakes and Edge Cases

A few behaviors of count() regularly surprise developers:

  • Counting null or scalars throws in PHP 8. In PHP 7.2–7.4, count(null) emitted a warning and returned 0, and counting a non-countable scalar returned 1. Since PHP 8.0, passing any non-countable value (null, a string, an integer) throws a TypeError. Guard the value first when it might not be an array.
  • count() is not a string length function. To count the characters in a string use strlen(), not count().
  • Only the top level is counted by default. As shown above, nested arrays count as a single element unless you pass COUNT_RECURSIVE.
<?php

$value = null;

// Safe even when $value is not an array:
$total = is_array($value) ? count($value) : 0;

echo $total; // Output: 0

Use is_array() to make sure a value is countable before calling count() on data whose type you don't control (for example, decoded JSON or a database row).

Conclusion

The count() function is a small but essential tool for working with arrays in PHP. Use it to size a list before looping or paginating, reach for COUNT_RECURSIVE when you need a full count across nested arrays, and remember that in PHP 8.0+ you must pass a real array (or a Countable object) — guard uncertain values with is_array() first. With those rules in mind, count() is a reliable, readable way to measure the data structures your code depends on.

Practice

Practice
What is the functionality of the 'count()' function in PHP?
What is the functionality of the 'count()' function in PHP?
Was this page helpful?