PHP count() Function

❮ PHP Array Reference

Example

Return the number of elements in an array:

<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Try it Yourself »

Definition and Usage

The count() function returns the number of elements in an array.


Syntax

count(array, mode)

Parameter Values

Parameter Description
array Required. Specifies the array
mode Optional. Specifies the mode. Possible values:
  • 0 - Default. Does not count all elements of multidimensional arrays
  • 1 - Counts the array recursively (counts all the elements of multidimensional arrays)


Technical Details

Return Value: Returns the number of elements in the array
PHP Version: 4+
PHP Changelog: The mode parameter was added in PHP 4.2

More Examples

Example

Count the array recursively:

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

echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
Try it Yourself »

❮ PHP Array Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.