PHP count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, count() returns the number of items in an array.

It can also count the items of an object that implements the Countable interface. With COUNT_RECURSIVE it counts the items inside nested arrays as well.

Syntax

Call count() with the values you need. Start with this line.

PHP
$total = count($fruits);
CallWhat it does
count([1, 2, 3])Returns 3.
count([])Returns 0.
count($matrix, COUNT_RECURSIVE)Counts every item in nested arrays.

sizeof() is an alias of count(). Since PHP 8, passing a value that is not an array or Countable object (like null) throws a TypeError.

Library declarations
PHP
count(Countable|array $value, int $mode = COUNT_NORMAL): int

Parameters

ParameterDescription
valueRequired. An array or an object that implements Countable.
modeOptional. COUNT_NORMAL (default) counts the top level only. COUNT_RECURSIVE counts all items in nested arrays too.

Return Value

Return ValueDescription
intThe number of items in the array or Countable object.

PHP Examples

Example 1 — Count Items

Find out how many items are in an array.

PHP
<?php
$fruits = ["apple", "banana", "cherry"];

echo "Fruits: ", count($fruits), PHP_EOL;
echo "Empty: ", count([]), PHP_EOL;

How It Works

  1. Create an array of three fruits.
  2. count() returns the number of elements, which is 3.
  3. An empty array has no items, so its count is 0.
  4. echo prints both numbers.

Example 2 — Recursive Counts and Errors

COUNT_RECURSIVE counts nested items, and a non-array value throws a TypeError.

PHP
<?php
$matrix = [1, [2, 3], [4, [5]]];
echo count($matrix), PHP_EOL;
echo count($matrix, COUNT_RECURSIVE), PHP_EOL;

try {
    count(null);
} catch (TypeError $e) {
    echo "Cannot count a non-countable value", PHP_EOL;
}

How It Works

  1. The normal count sees three top-level items: 1, [2, 3] and [4, [5]].
  2. The recursive count adds the nested items: 3 + 2 + 2 + 1 = 8, including the inner arrays themselves.
  3. count(null) throws a TypeError in PHP 8.
  4. The catch block prints a friendly message instead of crashing.

Example 3 — Average of a List

count() pairs naturally with array_sum().

PHP
<?php
$scores = [80, 90, 100];

$average = array_sum($scores) / count($scores);

echo "Average: ", $average, PHP_EOL;

How It Works

  1. Store three test scores in an array.
  2. array_sum() adds them and returns 270.
  3. count() returns 3, the number of scores.
  4. 270 divided by 3 gives an average of 90.

Frequently Asked Questions

It returns how many items are in an array or in a Countable object.
None. sizeof() is an alias of count().
From PHP 8, count() throws a TypeError if the value is not an array or Countable object, for example null or a string.
Pass COUNT_RECURSIVE as the second argument. It counts all the items in nested arrays, including the inner arrays themselves.
Use strlen() for characters in a string. count() is only for arrays and Countable objects.

Next: current()

current() returns the value of the array element that the internal pointer is pointing at.

current() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful