How to get number of elements in an Array? explain with example

Posted by

PHP offers count() and sizeof() functions. The count() and sizeof() both functions are used to count all elements in an array and return 0 for a variable that has been initialized with an empty array. These are the built-in functions of PHP. We can use either count() or sizeof() function to count the total number of elements present in an array.

Syntax:

count(array)

Example:-

<?php
  $days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 

  echo 'Total number of elements in the $days array is - ' . count($days);
  echo "<br>";
  echo 'Total number of elements in the $days array is - ' . sizeof($days);
?>  

Output:-

Total number of elements in the $days array is - 7
Total number of elements in the $days array is - 7

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x