PHP count() Function
Example
Returns the number of elements in the array:
<?php $cars=array("Volvo","BMW","Toyota"); echo count($cars); ?>
Definition and Usage
The count() function returns the number of elements in the array.
Syntax
count(array,mode);
Parameter | Description |
---|---|
array | Required. Specify the array. |
mode |
Optional. Specify the mode. Possible values:
|
Description
The count() function calculates the number of elements in the array or the number of properties in the object.
For arrays, returns the number of elements, for other values, returns 1. If the parameter is a variable and the variable is not defined, then returns 0.
If mode If set to COUNT_RECURSIVE (or 1), it will recursively calculate the number of elements in the arrays of the multidimensional array.
Technical Details
Return Value: | Returns the number of elements in the array. |
PHP Version: | 4+ |
Update Log: | mode The parameter was added in PHP 4.2. |
More Examples
Example 1
Recursively count 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); ?>