Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The PHP count() function is used to determine the number of elements in an array or countable object. It is commonly used in PHP applications to manage and analyze data efficiently. This function helps in looping through arrays and handling conditional checks based on element count.
In this article, you will learn about the syntax of the count() function, its parameters, and how to use it effectively in PHP.
Definition and Usage
In PHP, the array_count function is used to count the number of elements in an array. This function is very useful when you need to know how many items are in a list or a collection of data. Let's see how this function works and how you can use it in your code.
To use the array_count function, you first need to have an array. An array is a data structure that holds multiple values. Let’s take a look at the simple example of how you can create an array and count its elements:
<?php
// Create an array
$fruits = array("apple", "banana", "cherry", "date");
// Count the number of elements in the array
$fruit_count = count($fruits);
// Display the count
echo "There are $fruit_count fruits in the array.";
?>
You can also try this code with Online PHP Compiler
In this example, we create an array called $fruits with four elements. We then use the count() function to find out how many elements are in the array. The result is stored in the variable $fruit_count, which we then display.
The count() function is easy to use. It takes an array as an argument and returns the number of elements in that array. This function is essential when you need to perform operations based on the size of the array, such as looping through the array or checking if it's empty.
Syntax
The count() function is used to count the number of elements in an array. It has the following syntax:
count(array, mode)
array: The array whose elements you want to count.
mode (optional): It can take the following values:
0 (default): Counts only the elements in the first level of the array.
1: Counts elements recursively, including elements inside sub-arrays.
If an array contains sub-arrays, the count() function, by default, does not count the elements inside those sub-arrays. However, by passing 1 as the second argument, we can count all elements recursively.
2 (Counts only main keys)
5 (Counts all elements inside sub-arrays)
Difference Between count() and sizeof() Methods
PHP provides two functions to count elements in an array: count() and sizeof(). These two functions are nearly identical in behavior, but there are slight differences in how they are used.
From the above examples, we can see that sizeof() is just an alias for count() but does not support recursive counting. Therefore, count() is preferred in most cases.
Frequently Asked Questions
What is the difference between count() and sizeof() in PHP?
Both count() and sizeof() return the number of elements in an array. However, count() supports recursive counting when used with mode 1, while sizeof() does not.
How do I count all elements, including nested arrays?
You can use count($array, COUNT_RECURSIVE) to count elements in a multidimensional array, including elements inside nested arrays.
Can count() be used with objects?
Yes, count() can be used with objects that implement the Countable interface. Otherwise, it will return 1 for non-iterable objects.
Conclusion
In this article, we learned about the PHP count() function, which helps in counting elements in an array or an object. We explored its usage, parameters, and how it handles different cases. Understanding this function makes working with arrays easier and more efficient. Using count() correctly improves code readability and helps manage data effectively in PHP applications.
Live masterclass
Crack GenAI Skills to ace 30 LPA+ roles at Amazon & Google
by Sumit Shukla
16 Feb, 2026
03:00 PM
Zero to Google Data Analyst: Roadmap for 30L+ CTC
by Prashant
15 Feb, 2026
06:30 AM
Beginner to GenAI Engineer Roadmap for 30L+ CTC at Amazon
by Shantanu Shubham
15 Feb, 2026
08:30 AM
Data Analysis for 20L+ CTC@Flipkart: End-Season Sales dataset
by Sumit Shukla
16 Feb, 2026
01:30 PM
Crack GenAI Skills to ace 30 LPA+ roles at Amazon & Google