Understanding the count function _ PHP instance of php array

Source: Internet
Author: User
The following small series will bring you an in-depth understanding of the count function of PHP arrays. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian. 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 small editor. I hope to give you a reference, and I hope you can also support the script home.

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.