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

How to use the count function in php

王林
王林Original
2021-02-22 17:22:143129browse

Usage of count function in php: [count(array, mode)], where the parameter array specifies the array to be counted, and mode specifies the mode of the function. The count function is used to return the number of elements in an array.

How to use the count function in php

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

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 - Count the number of elements in an array recursively (count all elements in a multidimensional array).

Example:

<?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);
?>

Related recommendations:php tutorial

The above is the detailed content of How to use the 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