Open In App
Related Articles

PHP sizeof() Function

Improve Article
Save Article
Like Article

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.


int sizeof(array, mode);

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

  • array: This parameter represents the array containing elements that we need to count.
  • mode: This is an optional parameter and specifies the mode of the function. It can take two different values as shown below: 
    • 0: It is the default, does not count all elements of multidimensional arrays
    • 1: It Counts the array recursively (counts all the elements of multidimensional arrays)

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.


   // input array
   // getting total number of elements
   // present in the array.
   $result = sizeof($a);



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


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




Last Updated : 02 Dec, 2021
Like Article
Save Article
Similar Reads
Related Tutorials