Home > Article > Backend Development > How to use count function
PHP count() function is used to count the number of cells in an array or the number of attributes in an object.
php count() function syntax
Function: Return the number of elements in the array
Syntax:
count(array,mode);
Parameters:
array required. Specifies an array.
mode Optional. Specify the mode. Possible values: 0 - default. Do not count all elements in a multidimensional array. 1 - Recursively count the number of elements in an array (count all elements in a multidimensional array).
Description: For an array, return the number of its elements, for other values, return 1. If the argument is a variable and the variable is not defined, 0 is returned. If mode is set to COUNT_RECURSIVE (or 1), the number of array elements in a multidimensional array is recursively counted.
php count() function example 1
<?php $a = array("class" => "php中文网","name" => "西门","job" => "讲师"); print_r(count($a)); ?>
Output:
3
php count() function example 2
<?php $cars=array ( "Volvo"=>array ( "XC60", "XC90" ), "BMW"=>array ( "X3", "X5" ), "Toyota"=>array ( "Highlander" ) ); echo "常规计数:" . count($cars)."<br>"; echo "递归计数:" . count($cars,1); ?>
Output:
常规计数:3 递归计数:8
This article is an introduction to the PHP count function. I hope it will be helpful to friends in need!
The above is the detailed content of How to use count function. For more information, please follow other related articles on the PHP Chinese website!