News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Blake Gardner:
Practical usage of PHP 5.5 generators yield keyword
June 24, 2013 @ 11:54:42

With the release of PHP 5.5 came a whole group of new features, including the "yield" keyword for better handling of values in iteration. Blake Gardner has posted a practical example of its use to his site today.

The key to understating the way the yield works verses a normal function is that rather than generating all of your data and returning the final array when it's done; you yield the value as it's generated. The state of the generator function is saved after you yield and then its state is restored when called again so the iteration can continue.

He shows a basic use of "yield" in a simple foreach of 1000000 values. In the first example, memory is exhausted and the second yields the values as they come, reducing the overhead significantly. The "range_yield" function returns them as the "for" loop generates them.

0 comments voice your opinion now!
yield feature practical use tutorial generator

Link: http://blakegardner.co/2013/06/24/practical-usage-of-php-5-5-generators-yield-keyword

blog comments powered by Disqus

Similar Posts

PHPBuilder.com: Use PHPUnit to Implement Unit Testing in Your PHP Development

Avnet Labs Blog: TDD with Zend Framework

9Lessons.info: Facebook Style Tag Friends with Jquery, Ajax and PHP

IBM developerWorks: Developing PHP the Ajax way, Part 2 - Back, Forward, Reload

Wb Developer Juice: 10 baby steps to install Memcached Server and access it with PHP


Community Events

Don't see your event here?
Let us know!


introduction api library conference performance extension series php7 podcast voicesoftheelephpant framework release laravel example interview symfony2 version opinion community xdebug

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework