In-depth understanding of the Count function in PHP, in-depth understanding of phpcount_php tutorials

Source: Internet
Author: User

In-depth understanding of the Count function in PHP, in-depth understanding of Phpcount


In PHP programming, when iterating over an array, it is often necessary to calculate the length of the array as the judgment condition for the end of the loop, and in PHP the operations of the arrays are very frequent, so count is a common function, and the following is a look at the concrete implementation of the Count function.

I have a more detailed comment on the PHP source on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.

Count

int count (mixed $array _or_countable [, int $mode = Count_normal])

The Count function computes the number of elements in an array or object.

For an object, if you have an SPL extension installed, you can invoke the Count function by implementing the countable interface. The countable interface has and has only one method Countable::count (), which returns the return value of the count () function.

Parameter description

Mode

If the parameter mode is set to Count_recursive (or 1), COUNT () evaluates the array recursively. This is especially useful when calculating multidimensional arrays.

If the first parameter is not an array or an object that implements the countable interface, the Count function returns 1.

Note: The Count function detects recursion to avoid an infinite loop, but returns the e_warning hint when it encounters an infinite recursion or gets a higher than expected value.

Run the sample

General application

$arr 1 = Array (1, 2, 3, 4, 5); $val 1 = count ($arr 1); 5

Multidimensional arrays

$arr 2 = Array (' Apple ', ' banana ', Array (' Cat ', ' camel '), ' dog '), $val 2_1 = count ($arr 2); 4$val2_2 = count ($arr 2, 1); 6

Numbers and strings

$str = "Hello World"; $int _val = 1; $val 3 = count ($STR); 1$val4 = count ($int _val); 1

Normal Object

Class User {  private $name;  Private $address;} $user = new User (), $val 5 = count ($user); 1$VAL6 = count (array) $user); 2

Array-like Object

Class User extends Arrayobject {    private $name;    Public Function __construct () {      $this->name = ' HHQ ';    }    Public Function GetName () {      return $this->name;    }    Public function count () {      return 2;    }  }  $user 2 = new User ();  $val 7 = count ($user 2); 2

Implementing the Countable interface object

Class User implements countable {public    function count () {      return 3;    }  }  $user 3 = new User ();  $val 8 = count ($user 3); 3

Run steps

Enter switch statement to detect parameter types

If NULL, returns 0 directly

If it is an array, call the Php_count_recursive function machine to select the number of array elements

If it is an object, first check whether the array object (Array-like object), if it is, evaluates the number of arrays of objects

Otherwise, if the object implements the countable interface, the countable count method is called

Finally, other types, such as integer arrays or strings, return 1.

SOURCE Interpretation

In the case of an ordinary array, the Count function calls the Php_count_recursive function to perform the following steps for its function:

If the current hash bucket is accessed more than 1 times, repeat recursion, dye back e_warning error

Otherwise, count the number of array elements in the current array layer

If there is a recursive parameter option, the recursive access continues

If the argument is an object type, the implementation will first determine whether handler is defined. And handler is the structure of the object in the PHP kernel, which contains the Count_elements field, which is actually a function. If an object behaves like an array, which is usually said to be Array-like object, then the Count_elements function is executed. The concrete implementation is the class inherits PHP Arrayobject, and implements the Count function in the class, specifically calls the Count function, if the class does not implement the Count function, then count returns 0, otherwise returns the return value of the object's count function.

If it is a different data type

1. String

2. Digital

3, the object Branch Two if the judgment is false, that is, there is no inheritance arrayobject and does not implement the countable interface.

These types all return 1.

It is important to note that if you need to calculate the number of properties of an object, you can convert the object to an array and then call the Count function. Such as:

$count _value = count (array) $user);

Summary

Read the Count function of the source process, in which one step stuck, that is, if (z_obj_ht_p (array)->count_elements) This step, because always can not write into this branch of the demo, search on the internet a lot of data is not, Therefore consulted Tipi's reeze, finally obtained the desired answer. Don't know to ask, haha.

http://www.bkjia.com/PHPjc/1133038.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133038.html techarticle In- depth understanding of the Count function in PHP, in-depth understanding of phpcount in PHP programming, when iterating through an array, it is often necessary to calculate the length of the array as the judgment condition for the end of the loop, and in PHP ...

  • 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.