What is the difference between count or sizeof function in PHP? explain with example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Count and Sizeof functions in PHP

The accumulation 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() function: The sizeof() function 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 function 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
$arr = array(
       "Javascript" => array(
           "C++",
           "Github"
       ),
       "Selenium"=>array(
           "Ruby"  
       ),
       "PHP"=>array(
           "CodeLobster"
       )
);
 
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 (
[Javascript] => Array (
[0] => C++
[1] => Github
)
[Selenium] => Array (
[0] => Ruby
)
[PHP] => Array (
[0] => CodeLobster
)
)
Sub elements of an array: 3
All elements of an array: 7

count() function: The count() function 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 function 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
$arr = array(
       "Javascript" => array(
           "C++",
          "Github"
       ),
       "Selenium" => array(
           "Ruby"  
       ),
       "PHP" => array(
           "CodeLobster"
       )
);
 
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 (
[Javascript] => Array (
[0] => C++
[1] => Github
)
[Selenium] => Array (
[0] => Ruby
)
[PHP] => Array (
[0] => CodeLobster
)
)
Sub elements of an array: 3
All elements of an array: 7

Related Posts

What will you learn from Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Error: MySQL shut down unexpectedly, How to solve this error on xampp?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

What is the requirement for learning Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to repair the corrupted table ‘global_priv’ in the localhost PHPMyAdmin database?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Whenever you’re updating your PHP form data and getting a warning: count(): error message on the above page? How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

If details values are not retrieving at your PHP form whenever you update/edit your form. How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x