Using the getRange function above, we can do this.
For example, we can send a value to our generator telling to stop execution or change the output. This means that generators allow us to inject values into them, maybe as a command or something. We can then go ahead and use the pair as we would do with any array like this. When using generators, we can yield key-value pairs like this. There are times when our data only make sense when they are key-value based. Don’t forget, our aim is speed while using less memory. The data doesn’t necessarily need to be large - generators are effective no matter how small a dataset is.
We should try to conserve memory as much as possible. We don’t want actions like this hogging all the memory.
There are times when we might want to parse a large dataset (it can be log files), perform computation on a large database result, etc. Note: Generators can only be used from a function. Given the appropriate time, the browser eventually displays the data. If you head over to your browser, you should see data being displayed on the page. yield is similar to return as it returns a value from a function, but the only difference is that yield returns a value only when it is needed and does not try to keep the entire dataset in memory. " ĭissecting the getRange function, this time, we only loop through the values and yield an output. But, this time, we will be creating a generator function. Let’s define the same function above, call it with the same value PHP INTMAX and run it again. This is not effective, and we do not want a single script to use up all our memory. Let’s ask ourselves these questions, is this really effective? Do we want a single script to hog all our server’s memory? The answers are no and no. Possible solutions that come to mind include going into php.ini and increasing memory_limit. Well, that’s a shame, PHP ran out of memory. The generator script throws a warning error. But this time, you’ll notice something different. After doing this, head over to the browser and refresh. Now, the upper range(max) of generated numbers is PHP INTMAX, which is the largest number that your version of PHP can reach.
But if we go back into our code and make a little change " The code is pretty much self-explanatory, and this definitely doesn’t look like much. So if we go to we should get something like this.
We can quickly spin up an inbuilt PHP server in the directory where we created the generator.php file: After creating the file, we add this little code snippet. Still a bit confused? An example is a good way to show generators in action.įirst, let’s quickly create a generator.php file that we will use throughout this tutorial. What are PHP Generators?Īdded to PHP in version 5.5, generators are functions that provide a simple way to loop through data without the need to build an array in memory. Well, this article is on PHP Generators, so why are we talking about speed? As you are soon about to find out, generators make a huge difference in speed and memory management. The faster your application, the better the user experience.