Course Recommendation:

PHP sizeof() Function

Example

<?php
Return the number of elements in the array:
$cars=array("Volvo","BMW","Toyota");;
?>

Run Instance

echo sizeof($cars)

Definition and Usage

The sizeof() function calculates the number of units in the array or the number of properties in the object. The sizeof() function is count()

Alias of the function.Note:

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

Syntaxarraysizeof(mode);
, Description
array Required. Specify the array.
mode

Optional. Specify the mode. Possible values:

  • 0 - Default. Do not count all elements in multi-dimensional arrays.
  • 1 - Recursively count the number of elements in the array (count all elements in multi-dimensional arrays).

Technical Details

Return Value: Return the number of elements in the array.
PHP Version: 4+

More Examples

Example 1

Recursively count the number of elements in the array:

<?php
$cars=array
  (
  "Volvo"=>array
  (
  "XC60",
  "XC90"
  ),
  "BMW"=>array
  (
  "X3",
  "X5"
  ),
  "Toyota"=>array
  (
  "Highlander"
  )
  );
echo "Normal Count: " . sizeof($cars)."<br>";
echo "Recursive Count: " . sizeof($cars,1);
?>

Run Instance