WEBVTT

00:00.560 --> 00:04.040
I will call you back again to another video lecture in PHP.

00:04.040 --> 00:09.860
And in this video lecture I'll go ahead and tell you about for loop in PHP.

00:16.790 --> 00:17.210
All right.

00:17.210 --> 00:21.410
So we looked at a while loop and we have dealt with a do while loop.

00:21.440 --> 00:23.810
Now let's go ahead and look at for loop.

00:23.840 --> 00:32.180
So for loop is another loop system you can use to iterate through uh information in PHP.

00:32.420 --> 00:36.920
So your program you can always use for loop or use while loop or do while loop.

00:36.920 --> 00:38.240
Now let's go ahead and see.

00:38.240 --> 00:40.520
This is a for loop.

00:40.520 --> 00:45.680
And uh anything inside here is what I'm going to generate.

00:45.860 --> 00:49.880
So now let's go ahead and say I have this while loop right in here.

00:49.880 --> 00:53.330
And that you ask me why didn't I delete this while loop?

00:53.480 --> 01:00.170
The reason why I left this while loop till now is because I want to use that to teach us about for loop,

01:00.320 --> 01:04.610
because while loop is the same thing with for loop.

01:04.700 --> 01:08.030
But if you are a beginner, you will be like why?

01:08.060 --> 01:09.170
What is going on?

01:09.170 --> 01:14.540
So just the same way and the same information, everything right in here is what we are going to use

01:14.540 --> 01:17.810
to generate whatever we have at the output.

01:18.290 --> 01:23.540
So in for loop we are going to write everything within the for loop system.

01:23.540 --> 01:28.940
Then you go ahead and execute the program inside the for loop.

01:29.390 --> 01:30.650
Now let's go ahead and see.

01:30.650 --> 01:33.680
The very first thing we have here is the gradient variable.

01:33.680 --> 01:36.230
And that is the first thing you do inside the for loop.

01:36.260 --> 01:40.250
Go ahead and declare I want to say num equal to one.

01:40.310 --> 01:42.770
You see that num equal to one.

01:42.770 --> 01:49.070
And then you give the condition we set y null is less than or equal to ten.

01:49.070 --> 01:50.450
So that is the next condition.

01:50.450 --> 01:55.040
So the next thing after the condition in for loop is setting a condition.

01:55.040 --> 01:57.950
And then you can have a semicolon at the end.

01:57.980 --> 02:06.620
Because we have one more thing to go and that the next is setting that condition and the you go ahead

02:06.620 --> 02:07.490
and give it a target.

02:07.490 --> 02:10.760
So the target is that it's going to increment one here.

02:10.760 --> 02:13.790
You're going to remove the semicolon at the end.

02:13.790 --> 02:17.600
So just the sentence we have right in here declaring the variable.

02:17.600 --> 02:25.370
Then set your conditions and uh giving it uh the target.

02:25.370 --> 02:29.120
And that is you want this to always increment.

02:29.420 --> 02:32.120
Always wanted to also decrement you.

02:32.120 --> 02:35.390
And then now we go ahead and print out Norm.

02:35.420 --> 02:38.570
I'll go ahead and copy this and put it right inside the for loop.

02:38.570 --> 02:42.080
So now I want to delete the while loop.

02:42.110 --> 02:42.920
And we have here.

02:42.950 --> 02:45.380
Now let's go ahead and run this code and check it out.

02:45.380 --> 02:48.680
So you see I run this code and I got exactly the same thing.

02:48.680 --> 02:52.790
So if I go ahead and check this to change it to seven I'll run this code.

02:52.820 --> 02:55.460
You can see I have 127.

02:55.610 --> 03:00.140
So you can see that what we have is just exactly the same thing.

03:00.170 --> 03:04.910
So for loop makes it so we clean easier and straightforward.

03:04.910 --> 03:13.130
So that is why for loop is more preferable to so many people out there than other loop systems, but

03:13.130 --> 03:14.510
none is better than the other.

03:14.540 --> 03:16.610
You can actually use any one of your choice.

03:16.790 --> 03:17.330
All right.

03:17.330 --> 03:25.100
So let's go ahead and use this for loop to actually do some practices in PHP.

03:25.220 --> 03:27.920
Let's go ahead and say we have this variable.

03:27.920 --> 03:32.300
Or for goals settings and goals scored by different players.

03:32.330 --> 03:32.930
All right.

03:32.960 --> 03:35.420
Now we're going to do is to check an array.

03:35.450 --> 03:39.440
So we're going to create an array of these goals by different players.

03:39.470 --> 03:41.930
Let's say the first one score 95

03:43.910 --> 03:49.250
2015 and 40 goals.

03:49.790 --> 03:53.570
So we have this 12345 and six.

03:53.780 --> 03:56.240
Our goal is to also be up to seven.

03:56.430 --> 03:57.600
So let me see.

03:57.600 --> 03:59.640
The next one is going to be dating goals.

03:59.730 --> 04:01.620
Alright, now if I save this.

04:01.620 --> 04:03.240
So this is an array of numbers.

04:03.270 --> 04:05.430
Go ahead and place a semicolon at the end of this.

04:05.430 --> 04:08.070
And we want to generate this array.

04:08.100 --> 04:10.110
Want to print out these.

04:10.110 --> 04:12.720
So how do we print this out.

04:12.720 --> 04:20.520
So what we need to do right here is instead of all saying we have set this starting from one okay.

04:20.550 --> 04:24.390
Now what we need is to set it to start from zero.

04:24.390 --> 04:29.310
Because if you remember the time I taught you about array, I told you that the very first number here

04:29.340 --> 04:32.070
is index zero okay.

04:32.100 --> 04:34.200
And next one is index one.

04:34.200 --> 04:36.810
And the next one is index two.

04:36.990 --> 04:43.410
Uh index three, index four, index five and index six.

04:43.410 --> 04:45.780
So let's go here to comments on this.

04:45.780 --> 04:50.370
So that uh, we are not going to run into an error when we print that out.

04:51.840 --> 04:52.080
All right.

04:52.110 --> 04:53.910
Now I want to show you this.

04:53.910 --> 04:56.760
And uh, we want to me start from index zero.

04:56.760 --> 04:59.940
That means none is going to start from zero and not one.

04:59.970 --> 05:00.570
Okay.

05:00.600 --> 05:14.430
And then we are going to now print out the same none less than or equal to the array the variable name.

05:14.430 --> 05:16.050
So the variable here is go.

05:16.080 --> 05:16.620
Okay.

05:16.650 --> 05:18.630
So you can actually copy this.

05:18.630 --> 05:21.570
So that is what we are going to give the conditions.

05:21.570 --> 05:25.410
So say this is counts.

05:27.720 --> 05:30.540
So counts goes.

05:30.930 --> 05:31.380
All right.

05:31.410 --> 05:33.210
That is only going to print out.

05:33.360 --> 05:34.740
And they're all good.

05:35.010 --> 05:40.800
So we don't just echo the none we call the variable I.

05:40.800 --> 05:43.080
We're going to insert the num right inside the variable.

05:43.080 --> 05:44.490
So go ahead and say echo.

05:44.490 --> 05:48.420
And then we're going to be index of the nome.

05:50.070 --> 05:54.330
So the index of the nome is what we're going to insert right in there.

05:54.330 --> 05:59.760
Because if we put the index 10123, it's going to print just one.

05:59.760 --> 06:01.680
But we want everything to be printed out.

06:01.710 --> 06:14.010
Now if we go ahead and run this and we say we have an error at a line 12 and it says undefined array

06:14.040 --> 06:15.450
key seven.

06:15.450 --> 06:19.440
So this is not an error right in there.

06:19.680 --> 06:22.170
It's a go ahead and refresh this.

06:24.930 --> 06:27.960
And uh there is no error there.

06:29.100 --> 06:29.940
Only swirl.

06:29.940 --> 06:32.760
And uh that is it.

06:32.970 --> 06:35.970
Now if we go ahead and uh, say.

06:38.310 --> 06:43.890
Let's say we want to print this less than the number, we can go ahead and refresh this.

06:43.890 --> 06:45.870
And, uh, that is off.

06:45.870 --> 06:53.370
So what happened right here is I said that this starts from zero, and then num should be less than

06:53.370 --> 06:54.090
Down count.

06:54.090 --> 06:57.450
That means is going to print from 0 to 13.

06:57.570 --> 06:58.140
All right.

06:58.140 --> 07:03.330
So we remove less than or equal to because this is not number.

07:03.330 --> 07:06.000
It was actually given a count.

07:06.000 --> 07:09.450
And that is why it was giving us an error right in there.

07:09.450 --> 07:13.260
So now we removed less than or equal sign.

07:13.290 --> 07:16.200
So it's going to just be less than sign okay.

07:16.230 --> 07:19.350
So we can actually print from 9 to 18.

07:19.350 --> 07:26.580
So if I take this to maybe if I want 67 uh, 23 I refresh this.

07:26.610 --> 07:28.320
We can actually print that out.

07:28.350 --> 07:29.400
You can see that.

07:29.430 --> 07:32.700
So that is it adding is very interesting.

07:32.700 --> 07:37.470
You can use this whole loop to iterate through, uh, arrays of numbers.

07:37.470 --> 07:38.970
And that is very good.

07:38.970 --> 07:40.410
So why not practice with that.

07:40.410 --> 07:42.270
Check it out and play around it.

07:42.270 --> 07:46.770
And if you have any questions, go ahead and use the question and answer section.

07:46.770 --> 07:49.500
And I'm going to get back to you as soon as possible.

07:49.530 --> 07:50.430
Thank you so much.

07:50.430 --> 07:53.520
And I'm going to see you in the next video lecture.
