Let's talk about the generator functions.
Start with a simple function.
That's job is to generate a couple of random numbers so we can iterate over them and to return an array.
So how can we do that.
Well we need a result variable so we can keep all the elements in the array.
Then we can use a loop one of the loops.
So in this case we just pass an argument of how many elements do we want.
And we just count down.
Next up we need to generate the random number and add it to the array.
And only then we can return this result this resulting array.
So later on we can for example call this function in a for each loop and then iterate over the results.
Now let's see how can we do that in a generator.
Way to first simplify this countdown function, and also to save as much memory as possible.
Right.
So the first change is the return type.
Generators return the generator type.
Now we can proceed with simplifying this.
So we get rid of this result variable.
We don't need to store results anywhere.
And the difference between the generator function and normal function is you don't use the return keyword,
you use yield and everything else stays the same.
So the way it works is now the countdown function would return something every single time you call
yield.
We don't have to wait for the whole array to be created.
Thus we can save memory.
We don't have to create a data structure to return it.
First, we can yield something from the function as we go.
Now let's do the practical part.
Create a generator PHP and a starting tag.
And let me start with some code already so we can have this countdown function return the traditional
way and R for each loop.
So to see the difference between this traditional function and a generator, I need to echo something
when we generate a new number.
So I'm going to say generating number and add a new line.
And also when we echo something when we echo the number before we do that I'm also going to say something.
This would be echoing number.
Now let me run this.
So you see how it works.
The numbers are generated first the five numbers that we are generating.
And only then they are echoed one by one.
So obvious things.
We need to keep those numbers someplace.
So we need to use up memory.
So this is a tiny example a very small array.
But you can easily imagine processing hundreds of thousands or even millions of records.
And every single time such code runs.
And when you run PHP on servers, you might have multiple clients running at the same time, so you
can see how we can inflate the memory usage if we need to process some huge amounts of data.
And this is what generators are for.
So let me first return a generator from here.
Let's get rid of the result variable altogether.
And instead.
Let's yield the results.
And now when I run it you immediately see the difference.
So we generate the number and we instantly echo it.
We generate another one, we echo, and so on and so on.
So this means that you can say that this function returns multiple times until until it doesn't.
So to summarize the generators are great if you are working with huge data sets and you'd like to be
efficient with memory.