PHP count()Functions
Complete PHP Array Reference Manual
Examples
Returns the number of elements in an array:
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Run Example »
Definition and Usage
The count() function returns the number of elements in an array.
Syntax
count(array,mode);
| Parameters | Description |
|---|---|
| array | Required. Specifies the array to count. |
| mode | Optional. Specifies the mode of the function. Possible values:
|
Technical Details
| Return value: | Returns the number of elements in an array. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | The mode parameter was added in PHP 4.2. |
More Examples
Example 1
Recursively counts the number of elements in an array:
<?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);
?>
$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);
?>
Run Example »
Complete PHP Array Reference Manual Other Extensions