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

How to use the count function in php

王林
Release: 2023-03-08 07:46:02
Original
3199 people have browsed it

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

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

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!

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