PHP5.5 Iteration Builder usage examples, php5.5 generator
This example describes the PHP5.5 iteration builder usage. Share to everyone for your reference, as follows:
PHP5.5 introduces the concept of iterative generators, the concept of iteration is already in PHP, but the iteration builder is a new feature of PHP, similar to the Iteration builder in Python3, to see how the Iteration builder for PHP5.5 is defined.
<?phpfunction xrange ($start, $end, $step = 1) {for ($i = $start; $i <= $end; $i + = $step) { yield $i; }}foreach (xrange (1, 1000000) as $num) { echo $num, ' \ n ';}
Note keywords: yield, it is this yeild keyword constructs an iterator, the function xrange is different from the previous function is here . The general case is return a value, and yield a value indicates that this is an iterator, each loop once this iterator generates this value, it is called the iteration generator, the iteration generator This function can be a foreach loop, each time a value is generated.
PHP5.5 constructs iterators by defining the way the class implements the iterator interface, and the yield constructs an iterator that saves system overhead by increasing performance .
The advantages of this approach are obvious. It allows you to handle large data sets without having to load into memory at once, or even to handle infinite streams of data.
As the above example shows, the function of this iterator is to generate a number from 1 to 1000000, the loop output, then the use of the previous way 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 use of on-demand generation, In other words, when you call xrange this iterator, the function inside doesn't really run until you iterate every time.
Take a look at the PHP website example:
<?phpfunction xrange ($start, $limit, $step = 1) {for ($i = $start; $i <= $limit; $i + = $step) { yield $i;
}}echo ' single digit odd numbers: ';/* * Note that a array 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, the function and range are the same, if using the range function, then the internal implementation of the function will store each iteration of the intermediate process, that is, each intermediate variable has a memory space, then the first program to use the memory space is large, and allocate memory, Reclaiming memory can cause the program to run longer. However, if you use the xrange function of the yield implementation, all of the intermediate variables in it will use only one memory $i, so that the time and space saved can be reduced.
So why does yield have such an effect? Lenovo's yield in Lua is the concept of a co-process. In the Lua language, when a program runs to yield, the context is recorded using a process, then the program operation is returned to the main function, and when the main function calls resume, the process is recalled to read the context of the yield record. This creates a multi-process operation at the program language level. PHP 5.5 Here is the same reason for yield, when the program runs to yield, the current program evokes the context of the process record, and then the main function continues to operate, but PHP does not use the same as the Resume keyword, but "in use when the time to evoke" the process. For example, the foreach iterator in the example above can evoke yield. So the above example can be understood.
More readers interested in PHP related content can view this site topic: "PHP Operations Office Document Tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (string) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- How to upgrade the php5.3 version to php5.4 or php5.5
- The difference between the PHP5.5 and the previous version of the empty function
- PHP5.5 How to use the Memcached service side in Windows installation
- Introduction to the usage of class-level constants in php5.5
- php5.5 new Array function Array_column use
- PHP can be used in the iterative search for breadcrumb navigation to find a family tree implementation method
- PHP uses recursion and iteration to implement a quick sort example
- An explanation of the internal execution process of the PHP iterator
- PHP iterator implementation of Fibonacci sequence functions
- An in-depth analysis of the iterator pattern of PHP design Patterns
http://www.bkjia.com/PHPjc/1111343.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111343.html techarticle PHP5.5 Iteration Builder usage examples, php5.5 builder This article describes the PHP5.5 iteration builder usage. Share to everyone for your reference, as follows: PHP5.5 introduced the iterative life ...