The count() function returns the number of elements in an array.
PHP count() Function has the following syntax.
count(array,mode);
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to count |
mode | Optional. | Specifies the mode. |
Possible values for mode:
Count element in an array
<?php
$cars=array("A","B","C","java2s.com");
echo count($cars);
?>
The code above generates the following result.
Count the array recursively:
<?php/* ww w .j a va 2 s.co m*/
$a=array(
"Java"=>array("int","double"),
"CSS"=>array("height","width"),
"PHP"=>array("form")
);
echo "Normal count: " . count($a)."\n";
echo "Recursive count: " . count($a,1);
?>
The code above generates the following result.