Content · Programming · PHP

PHP count() Function: Usage and Examples

Understand count() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to count elements in an array or an object implementing Countable. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The count() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is count() in PHP?

Count elements in an array or an object implementing Countable.

Its syntax is:

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

Parameters used by this operation:

  • $value — The value supplied for this argument affects the operation.
  • $mode — The value supplied for this argument affects the operation.

COUNT_RECURSIVE also counts nested arrays, and self-references raise warnings. count() rejects most non-countable objects and scalars in modern PHP.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$products = ["pen", "book", "eraser"];
echo count($products);

Output:

3

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$groups = [[1,2],[3,4]];
echo count($groups) . PHP_EOL;
echo count($groups, COUNT_RECURSIVE);

Output:

2
6

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Show how many matching records have been loaded into a result array

Show how many matching records have been loaded into a result array. Save the code in a PHP file and adapt the sample data to your project.

<?php

$openTickets = [["id" => 101], ["id" => 102], ["id" => 103]];
echo "Open tickets: " . count($openTickets);

Output:

Open tickets: 3

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

COUNT_RECURSIVE also counts nested arrays, and self-references raise warnings. count() rejects most non-countable objects and scalars in modern PHP.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
count()Count elements in an array or an object implementing Countable
Related functionssizeof() is an alias; is_countable() tests whether count() can be used.

Common Beginner Mistakes

MistakeCorrect Approach
Counting nested elements as top-level elementsUnderstand what the collection exposes to count() and whether recursive counting is supported.
Assuming counting changes the itemsCount only reports a number; it does not remove or consume ordinary arrays.
Counting a value of the wrong typeCOUNT_RECURSIVE also counts nested arrays, and self-references raise warnings. count() rejects most non-countable objects and scalars in modern PHP.

Key Takeaways

  • count() helps you count elements in an array or an object implementing Countable.
  • COUNT_RECURSIVE also counts nested arrays, and self-references raise warnings. count() rejects most non-countable objects and scalars in modern PHP.
  • sizeof() is an alias; is_countable() tests whether count() can be used.
  • Use the practical example as a starting point, but validate real application inputs.

count() is useful when you need to count elements in an array or an object implementing Countable. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.