How to get number of element in an array? explain with example?

Posted by

We can get total number of elements in an array by using count() and sizeof() functions.

Count() Function

The count() function is used to get the total number of elements in an array and it returns numbers of element in an array.

Syntax= count(array)

example

<?php
      $array1 = array(  'php','java','css', 'python','c++');
    $array2 = array
     (
      0 => 'php',  
      1 => 'java',  
      2 => 'css', 

      3 => 'python',
      4 => 'c++'
     );
  

   echo count( $array1)."<br />";
     echo count( $array2);

  
?>

output

5
5

Sizeof() Function

The sizeof() function is used to count the number of elements present in an array or any other countable object. it returns the number of elements present in an array.

syntax

sizeof(array)

example

<?php
      
    $array2 = array
     (
      0 => 'php',  
      1 => 'java',  
      2 => 'css', 

      3 => 'python',
      4 => 'c++'
     );
  

     echo count( $array2);

  
?>

output

5
  • Both are used to count elements in a array sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().
guest

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