Home >Backend Development >PHP Tutorial >Detailed explanation of usage examples of PHP5.5 iterative generator, php5.5 generator_PHP tutorial
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.
<?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:
<?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"; ?>
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.