PHP | sizeof() Function

The sizeof() function is a builtin 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 two parameters as shown in the syntax above and described below:



  1. array: This parameter represents the array containing elements which we need to count.
  2. 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 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.

Examples:

Input: array(1,2,3,4,5,6)
Output: 6

Input: array(1,2,5,6)
Output: 4

Below programs illustrates the sizeof() function in PHP:

  • Count number of elements in one dimensional array:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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);
    ?>

    chevron_right

    
    

    Output:

    6
  • Counting number of elements in multi-dimensional array:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $array = array('name' => array('Geeks', 'For', 'Geeks'),
                  'article' => array('sizeof', 'function', 'PHP'));
      
    // recursive count
    echo sizeof($array, 1); // output 8
      
    // normal count
    echo sizeof($array); // output 2
      
    ?>

    chevron_right

    
    

    Output:

    8
    2
    

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.