PHP sizeof() Function

PHP sizeof() Function

The sizeof() function in counts all elements and return the size of the array. This function is an alias of count().

Syntax

sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] ) 

Parameter

array_or_countable(required)– This parameter represents the input array or countable object.

mode(optional)- This parameter is used to specify the mode. The possible mode values are as follows:

  • If it is set to the integer value 0 (default value), it does not count all elements of multidimensional arrays
  • If set to integer value 1, it counts all the elements of multidimensional arrays recursively.

Return

This function returns the number of elements in the array.

It returns 1, if the parameter “array_or_countable” is neither an array nor an object.

It returns 0, if array_or_countable is NULL.

Example 1

 

Output

Array
 (
     [0] => Mango
     [1] => Banana
     [2] => Strawberry
     [3] => Apple
 )
 The size of the array is 4 

Example 2

 

Output

The size of the array is int(3)

Example 3

array ( "Roll No"=>"15CS1829", "Coruse"=>"Btech EC" )
   ,
   "Shivam"=>array( "Roll No"=>"15CS1019", "Coruse"=>"Btech Civil")
   , 
   "Chandler"=>array( "Roll No"=>"15CS1022", "Coruse"=>"Btech Mechanical")
  ); 
 //counting all the elements of multidimensional arrays
 echo "Recursive count: " . sizeof($students,1);
 ?> 

Output

Recursive count: 9