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