Home  >  Article  >  Backend Development  >  What is the usage of count function in php

What is the usage of count function in php

王林
王林Original
2021-09-09 17:08:434665browse

The usage of the count function in php is: [count(array,mode);], where the parameter array specifies the array to be counted, and the parameter mode specifies the mode of the function.

What is the usage of count function in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

The count() function in PHP is used to return the number of elements in an array. Its syntax is as follows:

count(array,mode);

Let’s take a look at its parameters:

array is a required parameter, specifying the array to be counted; mode is an optional parameter, specifying the mode of the function, possible values: 0 - default, do not count all elements in the multidimensional array; 1 - recursively count the number of elements in the array ( Count all elements in a multidimensional array).

Let’s learn how to use the count() function through an example:

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

The operation results are as follows:

Normal count: 3
Recursive count: 8

Recommended learning: php training

The above is the detailed content of What is the usage of count function in php. 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