[PHP source reading]count function, PHP source count function _php Tutorial

Source: Internet
Author: User
Tags php source code

[PHP source reading]count function, PHP source Count function


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

Count Mixed $array _or_countable $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 Array (' Cat ', ' camel '), ' dog '); $val 2_1 Count ($arr 2//  4$val 2_2Count($arr 2// 6

Numbers and strings

$str = "Hello World"; $int _val = 1; $val 3 Count ($str//  1$val 4Count($int _val// 1

Normal Object

class User {    private$name;     Private $address ;} $user New User (); $val 5 Count ($user//  1$val 6count(array$user // 2

Array-like Object

    class 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 Implements countable {        publicfunctioncount() {            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.

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

If this article is helpful to you, please click on the recommendation, thank you ^_^

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

More source articles, welcome to personal homepage Continue to view: Hoohack

http://www.bkjia.com/PHPjc/1130480.html www.bkjia.com true http://www.bkjia.com/PHPjc/1130480.html techarticle [php source reading]count function, PHP code count function in PHP programming, in the traversal of the array often need to first calculate the length of the array as the end of the loop to determine the condition, 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.