Latest web development tutorials

PHP count () function

PHP Array Reference Complete PHP Array Reference

Examples

Returns the number of elements in the array:

<?php
$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 可选。规定函数的模式。可能的值:
  • 0 - 默认。不计算多维数组中的所有元素。
  • 1 - 递归地计算数组中元素的数目(计算多维数组中的所有元素)。

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);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference