00:00
Loops are fundamental in any programming language, allowing us to repeat actions and work with collections of data.
00:06
Let's explore the different types of loops in PHP and see how they are commonly used.
00:11
We're going to start with the most common loop in PHP, which is the forEach loop.
00:16
And we're providing an array, like this colors array here.
00:19
And then we're saying for each iteration we want to assign this current value,
00:24
this one here, or this, or this, to a new variable.
00:28
And we give this a name, so this can be anything we like, but commonly you just use the singular name of your array.
00:34
And then we have here the body where we can work with our item.
00:39
And for now we're just going to dump out the current color in the loop.
00:44
And you can see we get three times the output, red, blue, and green.
00:48
So this loop is working.
00:50
Something also worth mentioning is that next to the item we have also access here to the key as well.
00:56
With this operator here and now we have access to the key.
01:00
And if we dump it out you can see we have numeric keys here with 0, 1, and 2.
01:05
But of course if you would use an associative array with named keys you would get the name of the key here as well.
01:12
So yeah, just good for you to remember if you need the keys in a loop you have access to it as well.
01:17
And let's also take a look at a simple real world example.
01:21
So we have here some items inside an array with an item name here and the price.
01:27
And let's say we want to sum those prices here.
01:30
So we can do this by creating first an empty totals number which is 0.
01:35
Then we're going to loop over our invoice items.
01:40
And let's call the singular one just item.
01:43
And now what we want to do here is we want to add to the totals the current value of totals.
01:48
Which could be different with each iteration plus our item price.
01:52
And we already learned how we have access to this item in the array.
01:56
And then the only thing left is to dump out the new totals.
02:00
And you can see we get a value and I think this is the correct one.
02:05
The only little thing to mention here is we have this short syntax here where we can use plus equals.
02:10
And then we get rid of this here.
02:12
And this would do just the same.
02:14
Just adding this here to the totals and then assign the value to this variable.
02:19
And yeah, that's the for each loop.
02:20
You are going to use this a lot.
02:22
And there are different ways you can use this.
02:24
For example, like we've seen here, we can just add something.
02:28
We can also use some method calls here.
02:30
Maybe save some stuff to the database and so on.
02:33
But yeah, we're going to learn about other approaches later.
02:37
Next to the for each loop, we also have a for loop.
02:40
And this is very interesting because you have way more control over the iteration.
02:44
So the way it works, we have three different expressions here which we need to provide.
02:48
First, we have the variable we want to work with with an initial value.
02:53
Commonly, it's being used with $i, but you can name it whatever you want.
02:57
And now we define how long you want this loop to run.
03:01
So in our case, let's say as long as $i is smaller than 5.
03:06
And then we can say after each iteration how we want to change the value.
03:11
And we can just use plus plus in order to increase it by one with every iteration.
03:17
All right, let's dump out again just the value and see what we get here.
03:24
We're looping over the first value is zero, as we can see.
03:27
And then it's got increased by one.
03:29
And we're just dumping out the new value.
03:32
And what's interesting here is with those expressions here, we have a lot of control
03:36
how we want to iterate over something.
03:38
So you can use it for iterating over arrays as well by using the count of an array.
03:43
But you have a lot of more possibilities.
03:45
So you can also create a simple counter here, for example,
03:50
by setting this to 10 at the beginning.
03:52
And then say we want to run it as long as it's bigger than zero.
03:57
And then we're going to decrease $i every time.
04:00
And now you can say we have this little counter going from 10 downwards and so on.
04:04
So yeah, pretty cool loop.
04:06
And yeah, you will find some nice, interesting ways in how you can use the for loop.
04:11
And last, we have also a while loop.
04:15
And here we're going to start with a condition.
04:17
We want to run this as long.
04:20
Let's say our count is smaller than five.
04:23
Of course, we need to provide the count first.
04:26
Let's say this is zero at the start.
04:28
And then we have our body here again where we can now do stuff.
04:33
And what we want to do is we want to dump out again the current count.
04:39
And what we also want is to increase the count.
04:42
And we're going to use the shortcut expression here.
04:45
And let's see what we get here.
04:47
Yeah, so the loop is running five times starting by zero with our initial value
04:51
and then till four because if it gets five, then the condition is not matched anymore.
04:57
So that's the value where you want to run something as long as a specific condition is true.
05:03
Similar to this, we also have a special do while loop.
05:09
It's a little bit similar, but it's also different.
05:11
So let me show you.
05:13
We need to get rid of this here and the expression here.
05:18
And now our content wouldn't be up here.
05:21
All right, so what's happening?
05:23
We are now saying we want to do something, the same thing as before,
05:26
but then we are checking the condition.
05:29
So the output is now exactly the same as before,
05:31
but the difference is now that this is run no matter what the initial value is.
05:36
So even if it's 100 or 1,000, which is way bigger than five,
05:40
you will see that it's running the first time but not the second time
05:44
because only after the first iteration it's checking this condition here.
05:49
So yeah, this is the difference to the while loop or just with the while loop.
05:53
You would check the condition first, but here the thing you're doing is what's being run first.
05:58
So it's good for you to remember because there are situations where you want to always run this
06:03
and then check the condition.
06:05
Okay, so these are the main loops which we have in PHP,
06:08
but there are also some specific ways to control the loop execution.
06:12
So we're going to bring in a new example with users.
06:16
And every user has a name and they have a newsletter field where we define
06:20
if they want to receive the newsletter or not.
06:23
So we're going to start by using a forEach loop again.
06:26
And let's call this item user.
06:30
And then we would now send this to a user with a specific object or a dependency.
06:36
We're going to talk about these in the next videos,
06:39
but for now we're just dumping out that we would do this.
06:42
Send newsletter to user and we already know how we can get a specific value.
06:49
And in this case, we want to get the name of the user.
06:52
And I think this should do it.
06:54
Yeah, and that's running three times for all of our users and we're sending out this newsletter.
06:59
All right, but yeah, we have this field for a reason.
07:01
We want to make sure that in this chain does not get the newsletter because she doesn't want to.
07:07
So different ways we can do this.
07:09
We could, for example, just check the field here.
07:12
So if user newsletter, this is our condition.
07:15
If this is true, then we want to send the newsletter.
07:19
Let's try this out and it seems to be working.
07:21
It's running for John and for Bob and not for Jane.
07:24
But you can see this gets a little bit messy and there are some nicer ways we can do this.
07:29
So we're going to keep this condition here.
07:33
And we're just moving the vardum back under here.
07:38
So what we can do now, we can also control if we want to continue with this current loop or not.
07:44
And we're saying if the newsletter is set to false, which we can use with the negation condition here.
07:50
And then we want to continue, move on to the next iteration.
07:55
And we should see the same result.
07:57
Yeah, the newsletter is sent to John and to Bob, but not to Jane because we are continuing.
08:02
Next to continue, we have also the break keyword, which is a little bit similar.
08:07
But the difference is that it really breaks the loop.
08:10
So for the first iteration, newsletter is true, it's sending out the newsletter.
08:14
For the second one, it's false and now it's breaking the loop.
08:17
So it's not getting any more to Bob.
08:20
So in this case, break wouldn't be the one that you're using.
08:23
But it's good for you to know that you have continue and break for you to control the execution of your loop.
08:31
Okay, I think that's mostly what I wanted to show you about loops.
08:34
One more glimpse into the future.
08:37
So here we have a little example where we have numbers, an array of numbers.
08:41
And then we want to get the numbers doubled.
08:44
So each item should get doubled and inside this double array.
08:47
And the way we're doing this, we're looping over the numbers and then we're adding it to the new array.
08:51
I'll stamp out the new double array.
08:56
And you can see it's working 2, 4, 6, 8, and 10.
08:59
So this is nice, but when you work with PHP and then with Level,
09:03
more you will see that we also have collections in Level, which are very handy.
09:07
And where you can do something very similar, but in a more elegant and more readable syntax.
09:14
The way we could do the same with Level collection would be,
09:18
we're using our array, but we're wrapping this now into a collection,
09:23
which is a class, we get a new instance, and then we can run some nice methods on it,
09:27
which really tells us what we're doing.
09:29
We're mapping over each item.
09:31
And here's what we do with each item.
09:33
We're just doubling this out.
09:35
And when you work more with Level, we'll see that collections are super powerful.
09:38
And especially when you have some more things that you need to iterate over your collection.
09:44
If you need to modify it in more ways, you will have here some more function,
09:48
and this will be super readable.
09:50
So yeah, just a glimpse into Level.
09:52
You're going to love collections a lot.
09:55
I can tell you that already.
09:57
But yeah, so far so good.
09:58
This is all about loops for this video.
10:00
And in the next video, we're finally going to take a look at classes in PHP.