Using the count() Function in PHP

In this tutorial, you will learn how to use the count() function in PHP.

PHP count function

The count() function in PHP allows you to count all elements within an array or an object that implements the Countable interface.

This function is often used within scripts to check if an array is empty or to get the array’s length.

In the following sections, we will show you the syntax for the count() function and how to use it within your PHP scripts.

Syntax for the count() function in PHP

Let us start by exploring the syntax of the count() function in PHP. This syntax tells you a few things about the function, such as the parameters it takes and the data it will return

The count() function takes two parameters. The first is the value you want to count. The second optional field allows you to change the function’s behavior.


Below is a list of the parameters this function accepts and what they are used for.

  • value (REQUIRED) – The value parameter is required and is where you will define an array or countable object you want to count the length of.
  • mode (OPTIONAL) – The mode parameter will allow you to configure the behavior of the count function. You do not need to set a value with this parameter.
    • COUNT_NORMAL or 0 – This is the default value and means PHP will not recursively count the array or object.
    • COUNT_RECURSIVE or 1 – By setting this mode, the PHP’s count() function will recursively search through objects like a multi-dimensional array.

The count() function in PHP will return an integer containing the number of elements in that object.

If the passed-in object doesn’t exist or is empty, then the count function will return 0.

Basic Usage of the count() Function

Let us start by exploring the basic way you will use the count() function within your PHP scripts. Simply put, we will only be using the required parameter.

To showcase this, we will create a simple PHP array with the name “example” that will contain five values (0, 1, 2, 3, 4, 5).

After creating the array, we use the count() function to get the length of our “example” array. Finally, the value returned by this function is printed by using the PHP echo statement.


If you were to run the above example, you will see that the count() function returned the number 6.


Recursively Counting an Object with the count() Function in PHP

Now that you know how PHP’s count() function works let us explore putting its second parameter to use.

By setting the second argument to “COUNT_RECURSIVE“, the count() function will recursively count items. This feature is especially useful when dealing with multi-dimensional arrays.

We start this example by creating a multi-dimensional array with the name “example” and filling it with sample data.

After declaring the array, we use PHP’s count() function. We pass in the “$example” variable for the first parameter. Then for the mode parameter, we set “COUNT_RECURSIVE“. The result from the count function will be output, thanks to the echo statement.


Thanks to using the “COUNT_RECURSIVE” option, the count() function will count every element of the array, including the embedded arrays.


Conclusion

At this point in the guide, you should now understand how to use the count() function in PHP.

This function helps get the length of an array or an object that implements the Countable interface.

Please comment below if you have any issues using the count() function.

If you liked this tutorial, explore our many other PHP tutorials. We also have a wealth of coding guides if you want to learn a new language.

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat Links

Official Links.

1. Chat 1.
2. Chat 2.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Archives

Super Club Radio

Mighty Deals