PHP Source read: Count function

Source: Internet
Author: User
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 code 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;    

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 another data type 1, the 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.

Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.

Support me to write more good articles, thank you!

Reward authors

Support me to write more good articles, thank you!

Choose a payment method

About the Author: hoohack

A rookie who's trying to make a personal homepage · My article · 15 ·

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