Introduction to the PHP generator

Source: Internet
Author: User

Generally when you iterate over a set of data, you need to create a data, assuming that the array is large, it consumes a lot of performance, or even causes memory shortage.

//  Fatal error:allowed Memory size of 134217728 bytes exhausted (tried to allocate + bytes) in E:\php\test\index.php on Line 5range(1, 100000000);

PHP5.5 implements the generator, each time an array element is returned with the yield keyword, and the execution function is paused, when executing the function next method, it will continue to execute from the last position of yield, as the following example (Hebei plastic Flooring | Shanghai kiddy), will only produce the intermediate variable $ I

functionXrange$start,$limit,$step= 1) {     for($i=$start;$i<=$limit;$i+=$step) {yield$i; }} foreach(Xrange (1, 9, 1) as $number) {    Echo"$number";}

Introduction to the PHP generator

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.