For example, this is how you may get the array length by the count function:
$arr_nums = array (5,7,9,11,13); echo "The size of array = ", count($arr_nums);
This will output the same as using the sizeof function:
$arr_nums = array (5,7,9,11,13); echo "The size of array = ", sizeof($arr_nums);
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:
Argument | Description |
array_or_countable | The $array_or_countable is the required parameter.
You may specify the array name there for which you need to get the length. |
mode |
|
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:
PHP code:
<?php $arr_mix_item = array ('This',1,'is',2,'count',3,'function'); echo "The mixed array size = ", count($arr_mix_item); ?>
Output:
The example of using count with for loop
In this example, the count function is used with the for loop to iterate through the array elements.
The purpose is to provide expression value in the for loop by count function to which value should the loop execute.
In each iteration, the value is incremented by 1 and the loop goes on till all elements of the array are displayed.
The code:
<?php //Assigning values to an array $arr_loop = array (11,22,33,44,55); //Iterating through array for ($i=0; $i <count($arr_loop); $i++){ echo "Current item: $arr_loop[$i] <br />"; } ?>
Output:
Current item: 22
Current item: 33
Current item: 44
Current item: 55
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:
PHP code:
<?php //Assigning values to an array $arr_sizeof = array (25,50,75,100,125); //Getting length of array echo "Total elements in the array = ", sizeof($arr_sizeof), "<br /><br />"; //Iterating through array for ($i=0; $i <sizeof($arr_sizeof); $i++){ echo "Current item: $arr_sizeof[$i] <br />"; } ?>
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 arrays as shown in the example below.
<?php $sports = array('outdoor' => array('Baseball', 'Cricket', 'Football'), 'indoor' => array('badminton', 'table tennis', 'volleyball')); // Count with default value i.e 0 echo ("Count with default value = " .count($sports) . "<br /><br />"); // Count wiht 1 or recursive count echo ("Count with 1/COUNT_RECURSIVE value = " .count($sports, COUNT_RECURSIVE)); ?>
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 the count and sizeof functions.
That means the sizeof uses the same as the count function underneath.