Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of PHP5.5 iterative generator, php5.5 generator_PHP tutorial

Detailed explanation of usage examples of PHP5.5 iterative generator, php5.5 generator_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:50768browse

Detailed explanation of usage examples of PHP5.5 iterative generator, php5.5 generator

This article describes the usage of PHP5.5 iterative generator. Share it with everyone for your reference, the details are as follows:

PHP5.5 introduces the concept of iterative generator. The concept of iteration has been in PHP for a long time, but iterative generator is a new feature of PHP. This is similar to the iterative generator in python3. Take a look at PHP5.5 How is the iterative generator defined.

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

Note the keyword: yield. It is this yield keyword that constructs an iterator. This is the difference between this function xrange and previous functions . Generally, a value is returned, and a value of yield means that this is an iterator. This iterator generates this value every time it loops, so it is called an iterative generator. The iterative generator function can perform a foreach loop, each time Produce a value.

Before PHP5.5, iterators were constructed by defining classes to implement the Iterator interface. Constructing iterators through yield will further improve performance and save system overhead.

The advantages of this method are obvious. It allows you to process large data collections without loading them into memory at once, and you can even process infinitely large data streams.

As shown in the above example, the function of this iterator is to generate numbers from 1 to 1000000 and output them in a loop. If you use the previous method to generate the numbers from 1 to 1000000 into an array, it will take up a lot of memory. Because all results are generated in advance instead of on demand when used, that is to say, when the xrange iterator is called, the functions inside are not actually run until each iteration.

Look at the example from the PHP official website:

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

xrange here is an iteration, and its function is the same as range. If the range function is used, the internal implementation of the function will store the intermediate process of each iteration, that is, each intermediate variable has a memory space, so first the program The memory space used is large, and allocating memory and recycling memory will lengthen the running time of the program. But if you use the xrange function implemented by yield, all intermediate variables in it only use one memory $i, so the time and space saved will become smaller.

So why does yield have such an effect? Thinking of yield in Lua, here is the concept of coroutine. In the Lua language, when the program reaches yield, a coroutine is used to record the context environment, and then the program operation rights are returned to the main function. When the main function calls resume, the coroutine will be re-awakened and the yield record will be read. context. This forms multi-coroutine operations at the programming language level. The same is true for yield here in PHP 5.5. When the program runs to yield, the current program calls up the coroutine to record the context, and then the main function continues to operate. However, the keyword like resume is not used in PHP, but "using When the coroutine is called up. For example, the foreach iterator in the above example can evoke yield. So the above example can be understood.

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php-oriented "Introduction Tutorial on Object Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • How to upgrade the php5.3 version to php5.4 or php5.5
  • The empty function of PHP5.5 and previous versions Differences
  • How to install and use memcached server in PHP5.5 on windows
  • Introduction to the use of class-level constants in php5.5
  • Using the new array function array_column in php5.5
  • PHP can be applied to the iterative search for family tree implementation method of breadcrumb navigation
  • php example of using recursion and iteration to implement quick sort
  • Detailed explanation of the internal execution process of PHP iterator
  • PHP iterator implements the function of Fibonacci sequence
  • In-depth analysis of the iterator pattern of PHP design pattern

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111343.htmlTechArticleDetailed explanation of usage examples of PHP5.5 iterative generator, php5.5 generator This article describes the iterative generation of PHP5.5 device usage. Share it with everyone for your reference, the details are as follows: PHP5.5 introduces iterative generation...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn