Home  >  Article  >  Backend Development  >  How to use count function

How to use count function

藏色散人
藏色散人Original
2019-02-12 11:24:364746browse

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

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

Output:

3

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

Output:

常规计数:3
递归计数:8

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!

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