PHP5.5 Iterative builder usage examples _php tips

Source: Internet
Author: User
Tags generator lua php database php website

The example in this article describes the PHP5.5 iterative builder usage. Share to everyone for your reference, specific as follows:

PHP5.5 introduces the concept of an iterative generator, the concept of iteration is already in PHP, but the iterative generator is a new feature of PHP, similar to the iterative generator in Python3, to see how the PHP5.5 Iteration Builder is defined.

<?php
function xrange ($start, $end, $step = 1) {for
    ($i = $start; $i <= $end; $i + = $step) {
      yield $i; c4/>}
foreach (xrange (1, 1000000) as $num) {
    echo $num, "\ n";
}

Note the keyword: yield, it is this yeild keyword that constructs an iterator, this function xrange the difference from the previous function is here . Usually return a value, and yield a value means that this is an iterator, each cycle of this iterator to generate this value, it is called an iterative generator, the iterative generator This function can be a foreach loop, each time produces a value.

PHP5.5 is a way to construct an iterator by defining a class to implement the iterator interface, which will improve performance and save system overhead by yield the construction iterator .

The advantages of this approach are obvious. It allows you to handle large data sets without having to load them into memory at once, or even to handle infinitely large streams of data.

As the example above shows, the function of this iterator is to generate numbers from 1 to 1000000, loop output, then using the previous approach is to generate a good 1 to 1000000 of the number into the array, will be very memory, because it is necessary to generate all the results in advance, rather than the time required to generate, That is, when you call xrange this iterator, the inside function does not actually run until you iterate each iteration.

Take a look at the PHP website example:

<?php
function xrange ($start, $limit, $step = 1) {for
  ($i = $start; $i <= $limit; $i = = $step) {
    yield $i;
  }
}
echo ' single digit odd numbers: ';
/
 * Note this is never created or returned,
 * which saves memory.
 */
foreach (xrange (1, 9, 2) as $number) {
  echo "$number";
}
echo "\ n";
? >

The xrange here is an iteration, function and range is the same, if the range function, then the internal implementation of the function will store the intermediate process of each iteration, that is, each intermediate variable has a memory space, then the first program to use a large memory space, and allocate memory, Reclaiming memory can cause the program to run longer. But if you use the Xrange function implemented on the yield, all of the intermediate variables in it will use only one memory $i, which will save you less time and space.

So why does yield have this effect? Lenovo's yield in Lua is a concept of the coprocessor. In the Lua language, when a program runs to yield, it records the context environment using a coprocessor, and then returns the program operation right to the main function, and when the main function calls resume, the process is revived and the context of the yield record is read. This forms a program language-level multi-process operation. PHP 5.5 Here yield is also the same truth, when the program runs to yield, the current program to evoke the context of the process, and then the main function continues to operate, but PHP does not use such as resume keyword, but "in the use of the time to arouse the" coprocessor. For example, the foreach iterator in the previous example can evoke yield. So this example above can be understood.

For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.