Wednesday, September 25, 2024
Google search engine
HomeLanguagesWhat is the difference between count() and sizeof() functions in PHP ?

What is the difference between count() and sizeof() functions in PHP ?

The collection objects in PHP are characterized by a length parameter to indicate the number of elements contained within it. It is necessary to estimate the length of an array in order to perform array manipulations and modifications. 

sizeof() method: The sizeof() method is used to calculate all the elements present in an array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

sizeof(arr, mode)

Parameters: This method accepts two parameters that are discussed below:

  • arr – The array to count the elements.
  • mode – Indicator to check whether or not to count all the elements –
    • 0 – Default. Does not count all elements of multidimensional arrays
    • 1 – Counts the array recursively (counts all the elements of multidimensional arrays)

PHP




<?php
$arr = array(
       "Java" => array(
           "SpringBoot",
           "Eclipse"
       ),
       "Python"=>array(
           "Django"   
       ),
       "PHP"=>array(
           "CodeIgniter"
       )
); 
  
print_r($arr);
print("<br>");
  
echo "Sub elements of an array: " 
      . sizeof($arr) . "<br>";
echo "All elements of an array: "
      . sizeof($arr, 1);
  
?>


Output:

Array ( 
    [Java] => Array ( 
        [0] => SpringBoot 
        [1] => Eclipse 
    ) 
    [Python] => Array ( 
        [0] => Django 
    ) 
    [PHP] => Array ( 
        [0] => CodeIgniter 
    ) 
)
Sub elements of an array: 3
All elements of an array: 7

count() method: The count() method is used to calculate all the elements in the array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

count(arr, mode)

Parameters: This method accepts two parameters that are discussed below:

  • arr – The array to count the elements.
  • mode – Indicator to check whether or not to count all the elements –
    • 0 – Default. Does not count all elements of multidimensional arrays
    • 1 – Counts the array recursively (counts all the elements of multidimensional arrays)

PHP




<?php
$arr = array(
       "Java" => array(
      "SpringBoot",
      "Eclipse"
       ),
       "Python" => array(
           "Django"   
       ),
       "PHP" => array(
           "CodeIgniter"
       )
); 
  
print_r($arr);
print("<br>");
  
echo "Sub elements of an array: " 
      . count($arr) . "<br>";
echo "All elements of an array: "
      . count($arr, 1);
  
?>


Output

Array ( 
    [Java] => Array ( 
        [0] => SpringBoot 
        [1] => Eclipse 
    ) 
    [Python] => Array ( 
        [0] => Django 
    ) 
    [PHP] => Array ( 
        [0] => CodeIgniter 
    ) 
)
Sub elements of an array: 3
All elements of an array: 7

Difference between sizeof() and count() methods:

  • The sizeof() method takes a longer execution time.
  • The sizeof() method is an alias of the count() method.

Let us see the differences in a tabular form -:

  sizeof() count()
1. The sizeof() function is used to return the number of elements in an array. The count() returns the number of elements in the array.
2.

Its syntax is -:

sizeof(array, mode)

Its syntax is -:

count(array, mode)

3. Its return value is of integer type. Its return value is of integer type.
4. This function is an alias of count() function. The count() function may return 0 for a variable which is not set.
5. It is supported in PHP version 4.0+ It is supported in PHP version 4.0+

RELATED ARTICLES

Most Popular

Recent Comments