Home > Backend Development > PHP Tutorial > How to use count function

How to use count function

藏色散人
Release: 2023-04-05 07:24:02
Original
4792 people have browsed it

PHP count() function is used to count the number of cells in an array or the number of attributes in an object.

How to use count function

php count() function syntax

Function: Return the number of elements in the array

Syntax:

count(array,mode);
Copy after login

Parameters:

array required. Specifies an array.

mode Optional. Specify the mode. Possible values: 0 - default. Do not count all elements in a multidimensional array. 1 - Recursively count the number of elements in an array (count all elements in a multidimensional array).

Description: For an array, return the number of its elements, for other values, return 1. If the argument is a variable and the variable is not defined, 0 is returned. If mode is set to COUNT_RECURSIVE (or 1), the number of array elements in a multidimensional array is recursively counted.

php count() function example 1

<?php
$a = array("class" => "php中文网","name" => "西门","job" => "讲师");
print_r(count($a));
?>
Copy after login

Output:

3
Copy after login

php count() function example 2

<?php
$cars=array
  (
  "Volvo"=>array
  (
  "XC60",
  "XC90"
  ),
  "BMW"=>array
  (
  "X3",
  "X5"
  ),
  "Toyota"=>array
  (
  "Highlander"
  )
  );
echo "常规计数:" . count($cars)."<br>";
echo "递归计数:" . count($cars,1);
?>
Copy after login

Output:

常规计数:3
递归计数:8
Copy after login

This article is an introduction to the PHP count function. I hope it will be helpful to friends in need!

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