How to get Array length in PHP?

To get the total number of elements in an array, you may use the PHP count or sizeof functions. For example, this is how you may get the array length by count function:

This will output the same as using the sizeof function:

Both functions return the size of an array (i.e. total number of elements) then which one to use? I will explain this, first, let me explain both these functions with examples.

PHP count function

The count function returns the size or number of elements in an array or something in an object. The count PHP function takes two parameters as shown in the syntax below:

int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

The $array_or_countable is the required parameter. You may specify the array name there for which you need to get the length.

The mode parameter is optional. The possible values for this parameter are 0 or 1. The 0 is the default value. The usage of value 1 is particularly useful for multidimensional arrays. It makes counting the array recursively.

The example of using count function

In the first example, I am using a one-dimensional array of mixed elements (numbers and strings). After array declaration, the count function is used to get the size of that array:

See online demo and code output

The example code:

The example of using count with for loop

In this example, the count function is used with the for loop for iterating through the array elements. The purpose is to provide expression value in the for loop by count function to which value should loop execute.

In each iteration, the value is incremented by 1 and loop goes on till all elements of the array are displayed:

See online demo and code output

The code:

Using sizeof function in for loop example

Now, let us use the sizeof function for getting the length of a PHP array. First, I will display the length of the array by using sizeof function.

This is followed by using sizeof in the for loop and displaying the array elements:

See online demo and code output

The PHP code:

The output as you execute this code:

Total elements in the array = 5

 

Current item: 25

Current item: 50

Current item: 75

Current item: 100

Current item: 125

Using the mode parameter in count function example

In all above examples, we omitted to use the second parameter i.e. mode. So, count/sizeof functions used the default value which is 0.

As mentioned earlier, the value 1, or you may also use COUNT_RECURSIVE value instead of 1, will recursively count the array. This is useful for getting the size of multi-dimensional array as shown in the example below. To see the difference, I used both default and COUNT_RECURSIVE values and displayed the array length:

The output of this code is:

Count with default value = 2

 

Count with 1/COUNT_RECURSIVE value = 8

PHP count vs sizeof: What is the difference?

As per PHP official documentation, there is no difference between count and sizeof function. The sizeof function is just the alias of count function.

That means the sizeof uses the same as count function underneath.