In-depth understanding of the count function of the PHP array, in-depth understanding of count_php tutorials

Source: Internet
Author: User

In-depth understanding of the count function of the PHP array, in-depth understanding of count


Count ()

The PHP count () function calculates the number of cells in an array or the number of properties in an object, returns the number of cells in an array, or the number of properties in an object.

Grammar:

int count (mixed var [, int mode]) if VAR is a non-array normal variable, returns 1 for non-existent, uninitialized, or empty array 0.

The optional parameter mode is set to Count_recursive (or 1), and count () counts the array recursively, which is especially useful for all cells that calculate a multidimensional array, but count () does not recognize infinite recursion. The default value for mode is 0.

Example:

<?phpecho count ($x);      Output: 0$a = 2;echo count ($a);      Output: 1$arr_age = Array (+, +); Echo count ($arr _age);    Output:3?>

sizeof () is the alias of this function.

In practice, some loops are often performed based on the size of the array, and it is recommended that count () be written outside the loop body:

<?php$arr_age = Array (+), $count = count ($arr _age), for ($i =1; $i <= $count; $i + +) {  echo "$i cycles";}? >

This does not have to perform the count () calculation every time the loop is performed, which of course is not required.

This in-depth understanding of the PHP array count function is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.

http://www.bkjia.com/PHPjc/1136647.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136647.html techarticle In- depth understanding of the count function of the PHP array, in-depth understanding count count () the PHP count () function is used to count the number of cells in an array or the number of properties in an object, return the number of cells in an array, or pair ...

  • Related Article

    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.