Home  >  Article  >  Backend Development  >  How to use php count function

How to use php count function

藏色散人
藏色散人Original
2019-05-28 13:16:143054browse

php The count function is used to return the number of elements in an array. Its syntax is count(array,mode). The parameter array is required and refers to the array to be counted.

How to use php count function

#How to use the php count function?

Definition and usage

count() function returns the number of elements in the array.

Syntax

count(array,mode);

Parameters

array Required. Specifies the array to count.

mode Optional. Specifies the mode of the function. Possible values:

0 - Default. Not counting all elements in a multidimensional array.

1 - Recursively count the number of elements in an array (count all elements in a multidimensional array).

Return value: Returns the number of elements in the array.

PHP version: 4

Change log: The mode parameter is new in PHP 4.2.

Example 1

Recursively count the number of elements in an 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);
?>

Output:

Normal count: 3
Recursive count: 8

Example 2

Return the number of elements in the array:

<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>

Output:

3

The above is the detailed content of How to use php count function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn