In-depth understanding of the count function of PHP array, in-depth understanding of count_PHP tutorial-php Tutorial

Source: Internet
Author: User
A deep understanding of the count function of PHP array and a deep understanding of count. A deep understanding of the count function of PHP arrays, and a deep understanding of the countcount () PHPcount () function is used to calculate the number of cells in an array or the number of attributes in an object, returns the number of elements in the array or provides a deep understanding of the count function of the PHP array.

Count ()

The PHP count () function is used to calculate the number of units in an array or the number of attributes in an object, and returns the number of units in an array or the number of attributes in an object.

Syntax:

Int count (mixed var [, int mode]) If var is a non-array common variable, 1 is returned, and 0 is returned for nonexistent, uninitialized, or empty arrays.

The optional parameter mode is set to COUNT_RECURSIVE (or 1), and count () recursively counts the array, which is particularly useful for calculating all units of a multi-dimensional array, but count () it cannot recognize infinite recursion. The default value of mode is 0.

Example:

<? Phpecho count ($ x); // output: 0 $ a = 2; echo count ($ a); // output: 1 $ arr_age = array (18, 20, 25); echo count ($ arr_age); // output: 3?>

Sizeof () is the alias of this function.

In practice, some cyclic operations are often performed based on the size of the array. we recommend that you write count () in vitro:

<? Php $ arr_age = array (18, 20, 25); $ count = count ($ arr_age); for ($ I = 1; $ I <= $ count; $ I ++) {echo "Nth $ I cycle" ;}?>

In this way, you do not have to execute count () for each loop. of course, this is not necessary.

The above in-depth understanding of the count function of the PHP array is all the content shared by the editor. I hope to give you a reference and support for the help house.

The count function of the distinct array. the count () PHP count () function is used to calculate the number of units in the array or the number of attributes in the object, and return the number of units in the array or the number of objects...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.