Open In App

PHP sizeof() Function

In this article, we will see how to get the length of the array using the sizeof() function in PHP. The sizeof() function is a built-in function in PHP and is used to count the number of elements present in an array or any other countable object.

Syntax:



int sizeof(array, mode);

Parameter: This function accepts 2 parameters which are described below:

Return Value: This function returns an integer value as shown in the syntax which represents the number of elements present in the array.



We will understand the concept of the sizeof() function through the examples.

Example 1: This example illustrates the count of the number of elements in the one-dimensional array.




<?php
   // input array
   $a=array(1,2,3,4,5,6);
 
   // getting total number of elements
   // present in the array.
   $result = sizeof($a);
 
   print($result);
?>

Output:

6

Example: This example illustrates the counting the number of elements in the multi-dimensional array.




<?php
  $array = array('name' => array('Geeks', 'For', 'Geeks'),
                'article' => array('sizeof', 'function', 'PHP'));
 
  // Recursive count
  echo sizeof($array, 1);
 
  // Normal count
  echo sizeof($array);
?>

Output:

8
2

Reference: http://php.net/manual/en/function.sizeof.php


Article Tags :