WEBVTT

1
00:00:00.360 --> 00:00:03.230
The next loop we'll look at is a for loop.

2
00:00:03.230 --> 00:00:06.270
For loops are the most
complex loops in PHP,

3
00:00:06.270 --> 00:00:08.965
because they use three
expressions instead of just one.

4
00:00:08.965 --> 00:00:12.137
[SOUND] The first expression
is executed once,

5
00:00:12.137 --> 00:00:15.400
unconditionally at
the beginning of the loop.

6
00:00:15.400 --> 00:00:20.490
At the beginning of each iteration
expression two is evaluated.

7
00:00:20.490 --> 00:00:25.560
This is the conditional part if it
evaluates to true the loop continues and

8
00:00:25.560 --> 00:00:27.870
the statement block is executed.

9
00:00:27.870 --> 00:00:32.660
If it evaluates to false
the execution of the loop ends.

10
00:00:32.660 --> 00:00:36.100
At the end of each iteration
expression three is executed.

11
00:00:37.540 --> 00:00:40.960
We actually used all these
pieces in our while loop but

12
00:00:40.960 --> 00:00:43.860
now we can put them together in one place.

13
00:00:43.860 --> 00:00:46.990
Complex doesn't need to mean difficult.

14
00:00:46.990 --> 00:00:50.660
Sometimes having something
more complex in one place

15
00:00:50.660 --> 00:00:55.440
is actually easier to read than
looking for all the simple pieces.

16
00:00:55.440 --> 00:00:59.830
Let's take a look at how we can write
our list of years using a for loop.

17
00:01:01.360 --> 00:01:05.508
Let's go back to the loops.php file,
and comment out the while loop for

18
00:01:05.508 --> 00:01:06.531
our learn array.

19
00:01:11.142 --> 00:01:15.267
Then let's go back up to the top
of our file, and add our for loop,

20
00:01:15.267 --> 00:01:17.000
right below our variables.

21
00:01:18.100 --> 00:01:21.370
We start with the keyword for, and

22
00:01:21.370 --> 00:01:24.840
then our parentheses followed
by the curly braces.

23
00:01:26.290 --> 00:01:32.924
Now we can add the expressions, The first
expression gives us our starting value.

24
00:01:32.924 --> 00:01:35.560
So we'll move the year into our for loop.

25
00:01:39.710 --> 00:01:44.202
The second expression is the conditional,
so we copy that from our while loop.

26
00:01:48.562 --> 00:01:54.280
The third expression is for telling the
loop how to update values after each loop.

27
00:01:55.290 --> 00:01:59.050
We already have the iterator as
part of the conditional, and a for

28
00:01:59.050 --> 00:02:02.470
loop will allow us to leave off
any of the three expressions.

29
00:02:02.470 --> 00:02:05.640
This means that the for
loop is perfectly valid now.

30
00:02:05.640 --> 00:02:08.110
So I'm gonna leave it like
this just to demonstrate.

31
00:02:09.350 --> 00:02:12.688
Now we can copy the statement
block from our while loop as well.

32
00:02:15.528 --> 00:02:17.589
And we're ready to test
this out in the browser.

33
00:02:21.069 --> 00:02:24.442
We see the years 1917 through 2016 again,

34
00:02:24.442 --> 00:02:28.139
because we're iterating
before displaying the year.

35
00:02:28.139 --> 00:02:32.081
Let's move the iteration to
the third expression of our loop.

36
00:02:35.061 --> 00:02:43.660
Instead of the ++ being
here, we'll add year++.

37
00:02:43.660 --> 00:02:50.260
Now when we refresh our browser,
we see 1916 through 2016 because

38
00:02:51.320 --> 00:02:55.820
the final expression is not executed
until the end of the for loop.

39
00:02:57.080 --> 00:02:58.420
Let's clean this up a little more.

40
00:02:59.630 --> 00:03:02.432
I could move the current year into the for
loop,

41
00:03:02.432 --> 00:03:05.811
by separating these first two
expressions with a comma.

42
00:03:09.432 --> 00:03:13.230
That means both these expressions
will run before our first loop.

43
00:03:14.500 --> 00:03:20.960
Let's refresh the browser,
and our page still works.

44
00:03:20.960 --> 00:03:23.872
Now instead of the current year I
could just use the date function.

45
00:03:36.386 --> 00:03:40.488
And instead of subtracting 100,
I'll just subtract 99.

46
00:03:43.368 --> 00:03:48.829
Now when we refresh the browser
we see 1917 through 2016.

47
00:03:48.829 --> 00:03:54.820
You'll often see for loops used when
looping through numbers such as these.

48
00:03:54.820 --> 00:03:58.060
You can also use a for
loop with indexed arrays

49
00:03:58.060 --> 00:04:02.500
as long as the keys are sequential and
no keys have been skipped.

50
00:04:02.500 --> 00:04:07.431
This will work well on my learn array
if I use the sort to re-index the keys,

51
00:04:07.431 --> 00:04:08.777
let's take a look.

52
00:04:13.837 --> 00:04:17.968
We'll comment this out, and
then we'll move down here.

53
00:04:17.968 --> 00:04:22.722
Instead of asort, I want to just use sort,

54
00:04:22.722 --> 00:04:30.005
then we'll set up the structure for
our for loop, for () { }.

55
00:04:30.005 --> 00:04:32.720
Then we can start adding our expressions.

56
00:04:34.540 --> 00:04:36.710
First we start with our initial variable.

57
00:04:36.710 --> 00:04:42.390
This is going to be an integer, so
we can name int or just i for short.

58
00:04:42.390 --> 00:04:47.420
Then we can set i = 0 since
our keys are zero indexed.

59
00:04:48.590 --> 00:04:50.680
Then we can add our conditional.

60
00:04:50.680 --> 00:04:53.300
We want to loop while i

61
00:04:55.030 --> 00:04:59.420
is less than the number of
items in the learn array.

62
00:05:00.430 --> 00:05:05.320
So we'll use another handy little function
named count, which does just that.

63
00:05:07.020 --> 00:05:10.020
It counts the number of
items in a given array.

64
00:05:10.020 --> 00:05:11.137
So we'll pass at the learn array.

65
00:05:13.078 --> 00:05:16.217
Then we add our final expression
to iterate through i.

66
00:05:18.558 --> 00:05:21.270
Now we're ready to display
each of those items.

67
00:05:22.470 --> 00:05:30.200
We start with echo $learn and
then within square brackets we use i.

68
00:05:32.020 --> 00:05:34.810
This holds the key value
that we want to use.

69
00:05:36.160 --> 00:05:41.138
Finally let's concatenate a line break.

70
00:05:41.138 --> 00:05:42.798
Now let's run the script in the console.

71
00:05:46.158 --> 00:05:50.040
We see our list of to do
items sorted by value again.

72
00:05:50.040 --> 00:05:50.540
Great job.
