What is the usage of count function in php

王林
Release: 2023-03-12 11:56:01
Original
4689 people have browsed it

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);
Copy after login

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);
?>
Copy after login

The operation results are as follows:

Normal count: 3
Recursive count: 8
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template