Home>Article>Backend Development> Introduction to the usage of count function in PHP array (with code)

Introduction to the usage of count function in PHP array (with code)

不言
不言 forward
2019-03-13 13:11:56 3223browse

This article brings you an introduction to the usage of the count function in PHP arrays (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP count() function is used to count the number of cells in an array or the number of attributes in an object, and returns the number of cells in the array or the number of attributes in the object.
Syntax:
int count(mixed var [, int mode]) Returns 1 if var is an ordinary variable that is not an array, and 0 for non-existent, uninitialized, or empty arrays.
Optional parameter mode is set to COUNT_RECURSIVE (or 1), count() will recursively count the array, which is especially useful for counting all cells of a multi-dimensional array, but count() does not recognize infinite recursion. The default value of mode is 0 .
Example:

sizeof() is the alias of this function.

In practical applications, some loop operations are often performed based on the size of the array. It is recommended to write count() outside the loop:

This way you don’t have to perform count() calculations every time you loop, of course this is not necessary.

The above is the detailed content of Introduction to the usage of count function in PHP array (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete