PHP55 new feature yield generator

Source: Internet
Author: User
PHP Official Document Yield http://php.net/manual/zh/language.generators.overview.php

Reference: http://laravelacademy.org/post/4317.html

The generator provides an easier way to implement a simple object iteration, comparing the way that the definition class implements the Iterator interface, with significantly reduced performance overhead and complexity.

The generator allows you to write code in a foreach code block to iterate over a set of data without having to create an array in memory, which will limit your memory to a maximum or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and the normal function returns only once differently, and the generator can yield multiple times as needed to generate values that need to be iterated.

$length=1000000;//Generatorforeach(Yieldtest ($length) as$v 1) {Echo$v 1;} functionyieldtest($length) { for($i=0;$i<$length;$i++) {yield$i.' Camel '; }}/* Iterator The following code throws an error: PHP Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate) bytes r/www/test/90/yield/demo01.php on line 38*/foreach(Iteratortest ($length) as$v 2) {Echo$v 2;} functioniteratortest($length) {$data= []; for($i=0;$i<$length;$i++) {$data[] =$i.' Camel '; }return$data;}

If you want to calculate a large amount of data in a specific way, such as manipulating Excel table data, it has more performance implications. At this point, we can use the generator to calculate and produce the subsequent values instantly, without taking up valuable memory space and using less memory.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP55 new feature yield generator, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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