WEBVTT

00:00.140 --> 00:02.720
Let's talk about the generator functions.

00:02.750 --> 00:05.330
Start with a simple function.

00:05.330 --> 00:13.820
That's job is to generate a couple of random numbers so we can iterate over them and to return an array.

00:14.300 --> 00:15.440
So how can we do that.

00:15.440 --> 00:20.480
Well we need a result variable so we can keep all the elements in the array.

00:21.050 --> 00:25.130
Then we can use a loop one of the loops.

00:25.130 --> 00:31.010
So in this case we just pass an argument of how many elements do we want.

00:31.040 --> 00:33.380
And we just count down.

00:35.060 --> 00:40.340
Next up we need to generate the random number and add it to the array.

00:41.120 --> 00:45.710
And only then we can return this result this resulting array.

00:45.740 --> 00:53.480
So later on we can for example call this function in a for each loop and then iterate over the results.

00:54.230 --> 00:57.710
Now let's see how can we do that in a generator.

00:57.710 --> 01:06.080
Way to first simplify this countdown function, and also to save as much memory as possible.

01:06.590 --> 01:07.850
Right.

01:08.030 --> 01:10.490
So the first change is the return type.

01:10.490 --> 01:14.420
Generators return the generator type.

01:14.660 --> 01:17.630
Now we can proceed with simplifying this.

01:18.110 --> 01:21.260
So we get rid of this result variable.

01:21.260 --> 01:24.110
We don't need to store results anywhere.

01:24.110 --> 01:31.190
And the difference between the generator function and normal function is you don't use the return keyword,

01:31.190 --> 01:35.810
you use yield and everything else stays the same.

01:35.810 --> 01:44.360
So the way it works is now the countdown function would return something every single time you call

01:44.390 --> 01:45.080
yield.

01:45.080 --> 01:48.680
We don't have to wait for the whole array to be created.

01:48.680 --> 01:50.600
Thus we can save memory.

01:50.630 --> 01:54.650
We don't have to create a data structure to return it.

01:54.650 --> 02:00.020
First, we can yield something from the function as we go.

02:00.920 --> 02:03.200
Now let's do the practical part.

02:03.650 --> 02:09.260
Create a generator PHP and a starting tag.

02:09.260 --> 02:17.090
And let me start with some code already so we can have this countdown function return the traditional

02:17.090 --> 02:20.330
way and R for each loop.

02:20.330 --> 02:28.310
So to see the difference between this traditional function and a generator, I need to echo something

02:28.310 --> 02:30.590
when we generate a new number.

02:32.870 --> 02:37.610
So I'm going to say generating number and add a new line.

02:39.140 --> 02:46.730
And also when we echo something when we echo the number before we do that I'm also going to say something.

02:46.730 --> 02:50.060
This would be echoing number.

02:50.510 --> 02:52.190
Now let me run this.

02:54.290 --> 02:56.150
So you see how it works.

02:56.210 --> 03:01.130
The numbers are generated first the five numbers that we are generating.

03:01.130 --> 03:04.730
And only then they are echoed one by one.

03:04.730 --> 03:06.530
So obvious things.

03:06.530 --> 03:09.050
We need to keep those numbers someplace.

03:09.050 --> 03:10.820
So we need to use up memory.

03:10.850 --> 03:14.900
So this is a tiny example a very small array.

03:14.900 --> 03:21.620
But you can easily imagine processing hundreds of thousands or even millions of records.

03:21.620 --> 03:24.350
And every single time such code runs.

03:24.350 --> 03:31.210
And when you run PHP on servers, you might have multiple clients running at the same time, so you

03:31.210 --> 03:38.680
can see how we can inflate the memory usage if we need to process some huge amounts of data.

03:38.680 --> 03:42.280
And this is what generators are for.

03:42.850 --> 03:46.480
So let me first return a generator from here.

03:46.510 --> 03:52.870
Let's get rid of the result variable altogether.

03:56.140 --> 03:57.580
And instead.

04:00.640 --> 04:02.770
Let's yield the results.

04:02.770 --> 04:07.750
And now when I run it you immediately see the difference.

04:07.750 --> 04:11.590
So we generate the number and we instantly echo it.

04:11.620 --> 04:16.210
We generate another one, we echo, and so on and so on.

04:17.200 --> 04:26.800
So this means that you can say that this function returns multiple times until until it doesn't.

04:26.830 --> 04:33.790
So to summarize the generators are great if you are working with huge data sets and you'd like to be

04:33.790 --> 04:35.320
efficient with memory.
