PHP count () function
Examples
Returns the number of elements in the array:
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Running instance »
Definition and Usage
count () function returns the number of elements in the array.
grammar
count( array,mode );
参数 | 描述 |
---|---|
array | 必需。规定要计数的数组。 |
mode | 可选。规定函数的模式。可能的值:
|
technical details
return value: | Returns the number of elements in the array. |
---|---|
PHP version: | 4+ |
Update log: | mode parameters are new in PHP 4.2. |
More examples
Example 1
Recursively compute the number of elements in the 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);
?>
Running instance »
Complete PHP Array Reference