From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Introducing PHP generators

Introducing PHP generators

- [David] Hi, I'm David Powers, and welcome to this week's edition of PHP Tips, Tricks, and Techniques, designed to help you become a smarter, more productive PHP developer. This week, I'm going to look at generators in PHP. Generators are relatively new. They first became available in PHP 5.5. They look like normal functions, but with a big difference. Instead of returning a single value and exiting, generators can yield a value and then pause. And each time the generator is called again, it yields the next available value until there are no more left. So, what are the advantages? Generators are simple iterators. In other words, they can be used inside a loop to generate multiple values. Because they use the same syntax as functions, there's no need to define a class. They frequently use less memory than calling a normal function repeatedly, and they can be used for advanced programming structures such as code routines and asynchronous PHP. In this video, we'll focus only on the…

Contents