Deep understanding of the Count function _php instance in PHP

Source: Internet
Author: User
Tags arrays php programming php source code

In PHP programming, in the traversal of the array often need to calculate the length of the array as the end of the cycle of judgment conditions, while in PHP, the operation of the array is very frequent, so count is a common function, the following study the implementation of the Count function.

I have a more detailed annotation of PHP source code on the GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. You can view the annotations that you have added through a commit record.

Count

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

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

For objects, if you install SPL extensions, you can invoke the Count function by implementing the countable interface. The countable interface has and only one method Countable::count (), which returns the return value of the count () function.

Parameter description

Mode

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

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

Note: The count function can detect recursion to avoid infinite loops, but will return e_warning hints when encountering infinite recursion or getting larger than expected.

Running 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
$val 2_2 = count ($arr 2, 1);//6

Numbers and strings

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

Normal Object

Class User {
  private $name;
  Private $address;
}

$user = new User ();
$val 5 = count ($user); 1
$val 6 = count ((array) $user);//2

Array-like objects

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 countable interface objects

Class User implements countable {public
    function count () {return
      3;
    }
  }

  $user 3 = new User ();
  $val 8 = count ($user 3); 3

Run steps

Enter switch statement detect parameter type

If NULL, return directly to 0

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

If it is an object, first check to see if it is an array object (Array-like object) and, if so, count the number of array 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 Code Interpretation

If it is an ordinary array, the Count function calls the Php_count_recursive function to perform its function as follows:

If the current hash bucket is recursively accessed more than 1 times, it shows repeated recursion, dyeing back e_warning error

Otherwise, the number of array elements for the current array layer is computed

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

If the parameter is an object type, the implementation will first determine whether the 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, that is, Array-like object, which is usually said, the Count_elements function is executed. The 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's a different type of data

1, string

2, Digital

3, the object branch in the case of two if judgment is false, that is, did not inherit Arrayobject and did not implement the countable interface.

All these types return 1.

Note that if you want to calculate the number of attributes for an object, you can convert the object to an array, and then call the Count function. Such as:

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

Summary

Reading the Count function in the source process, in one step stuck, that is, if (z_obj_ht_p (array)->count_elements) This step, because you can not write into this branch of the demo, the Internet search a lot of information is not fruit, Therefore consulted the Tipi reeze, finally got the answer that wants. Don't know to ask, haha.

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.