Current Location: Home> Function Categories> sizeof

sizeof

count alias
Name:sizeof
Category:Array
Programming Language:php
One-line Description:an alias for count().

Definition and usage

The sizeof() function calculates the number of units in an array or the number of attributes in an object.

sizeof() function is an alias for the count() function.

Note: When the variable is not set, or the variable contains an empty array, the function returns 0. You can use the isset() variable to test whether the variable is set.

Example

Example 1

Returns the number of elements in the array:

 <?php
$cars = array ( "Volvo" , "BMW" , "Toyota" ) ;
echo sizeof ( $cars ) ;
?>

Try it yourself

Example 2

Recursively calculate the number of elements in an array:

 <?php
$cars = array
  (
  "Volvo" => array
  (
  "XC60" ,
  "XC90"
  ) ,
  "BMW" => array
  (
  "X3" ,
  "X5"
  ) ,
  "Toyota" => array
  (
  "Highlander"
  )
  ) ;

echo "General Count:" . sizeof ( $cars ) . "<br>" ;
echo "Recursive Counting:" . sizeof ( $cars , 1 ) ;
?>

Try it yourself

grammar

 sizeof ( array , mode ) ;
parameter describe
array Required. Specify array.
mode

Optional. Prescribed mode. Possible values:

  • 0 - Default. Not counting all elements in a multidimensional array.
  • 1 - Recursively count the number of elements in the array (calculates all elements in a multidimensional array).
Similar Functions
Popular Articles