Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous chapter we looked at the first type of control structure, logical expressions, which let our code make choices about how to proceed. In this chapter, we're going to be looking at the other main type of control structure, loops. Loops allow us to write code that will execute more than once, without having to write out all of that code over and over again. The code executes once and then it loops back to the start to execute again. And each time the code executes, it doesn't necessarily do the exact same thing. The values of variables may be changed each time it goes through the loop. So that logical expressions inside the loop do different things.
And of course, a critical feature of any loop is knowing when to stop looping. We're going to take a look at three types of loops. While loops, for loops, and foreach loops. We're going to begin with while loops. I think these are the simplest. The syntax is simply while followed by an expression, that's the argument to it and then a statement. The statement is the loop, the thing we're going to do over and over again, while the expression evaluates to true. Now with a single line statement this will work. But most times you're going to have a multi-line statement and I also think it's just a good practice to always use those curly braces.
It makes it very clear what's inside the loop and what's not. Then notice how similar the syntax is to our if statement. In fact if you swapped out the word while for if, it'd be exactly the same. And the expression part works exactly the same way. It's going to be a Boolean expression that's going to evaluate to either true or false. So what happens is it says, while expression is equal to true. Do this block of code. Start at the top and do all of these statements and when you get to the bottom, come back to the top again and reevaluate the expression. Is it still true? If it is, go through and do all the statements again 'til you get to the last curly brace.
Then back to the top. Evaluate that expression again. Once it evaluates to false well, now we don't do it anymore, because we're only doing it while it's true. So now, the code jumps back down to the curly brace, it does not execute that final time, and we move on with the rest of our PHP code from there. Let me give you an example. So let's say that we have while, the variable count is less than or equal to 10. So as long as that's true, as long as count is less than or equal to 10, we're going to echo the count and then add 1 to it.
Then we'll go back to the top, and we'll check again, is count less than or equal to 10. And if it is less than or equal to 10, it will echo the count and then add 1 to the count and go back to the top again. So you can see what's going to happen here. Let's say the count started out being 0 well, the first time through, count is less or equal to 10, so it executes. This time count is equal to one, still less or equal to 19, so the loop executes. It keeps on going until finally count is equal to up to 11. When count is equal to 11, it's no longer less than or equal to 10.
It's false. And at that point it stops executing the loop. Now, two things, about the loop are critical. The loop needs to have, a condition, an expression, that will allow it to execute. Number two something about the condition has to change during the course of the loop. If not you'll end up getting an infinite loop. Let me show you what I mean. Lets say that we didn't have count plus equal 1 in there. We just took that line out. Well now the first time through lets say count is equal to three. Three is less than or equal to 10 so we'll echo three.
Go back to the top of the loop, 3 is less than or equal to 10, so we'll echo 3. It'll just keep going and you'll just keep outputting 3's over and over and over again. But we have to have some relationship between what's happening inside the loop and the condition that's above it. Something has to eventually change so that now the behavior of the loop will be triggered and we won't just be going infinitely through it. Let's try some examples for ourselves. So to start with, I'm going to open basic.html, just some simple html that I've created, and I'll do save as.
And we're going to call this whileloops.php. I'll call it loops, while. So, let's just try our very first loop here. And let's do the example that we just had. Let's do count equals zero, and then while and then our expression. You could have the space here or not. And then while count is less than or equal to 10. And then inside our curly braces, what do we want to do each time? So until count gets to 10 we're going to echo count, I'm going to go ahead and put a comma after it.
Cartenate the comma with a period, and then I'm going to do count and I'm going to use plus plus just to show you. Remember on the slide I did it using count plus equals 1. Well count plus plus does the exact same thing, it's just increments count. Remember that? So, I'll just make a note here Increment by 1 just so we don't forget. Now let's save it and let's try it out. Let's come back over here to Firefox and I'm going to go to local host, and for me, it's going to be Kevin Skoglund. And then in my sandbox, and then I just made this one called whileloops.php.
So, there it is. See what I got back? The value started out as 0. Let's just bring these up together so we can see them at the same time. So, the first time through count was 0. So, it executes, count gets output. Now, count becomes 1. Goes back to the top of the loop. Still meets the condition, still, the expression evaluates to true. So it outputs it again, keeps incrementing. Finally, let's look at the top. When we get to nine, it's, nine is less than or equal to 10, so it outputs, it becomes 10.
10 is less than or equal to 10, so it outputs the 10, and then it becomes 11. It goes back to the top. Now count is equal to 11, which is not less than or equal to 10. So, it does not execute this last time, it drops right down to here. And just to make sure that you see this, let's do echo br. And echo, count colon then let's output the value of count here. So, after the loop is completely done, let's take a peak at what count is at that last time.
See, there it is, it's equal to 11 after the loop is done. Now, as I said, going through a loop doesn't have to do the same thing each and every time. I mean obviously the values did change and it went from 0 all the way up to 10, which was nice. We had one little bit of very simple code that actually did something different each time through the loop. We can do even more than that, let's do an If statement. Let's say, if the count is equal to 5, then I'll put my curly braces. And I'm just going to go ahead and do else.
And I'm going to copy this and paste it in here. Alright. So, if it's not equal to five. The else statement is, it's going to do what it was doing before. But for, if it's five, let's have it echo the word, five, with a comma after it. Save it and let's come back here and we'll reload and you'll see that it pops up and it does something different when it gets to 5. This is a very simple example, but if you think for a second, you can see how it would be applicable. How you would say, alright if I'm outputting a set of pictures every fifth picture, I want to put in a different HTML.
Maybe you want to do a line break after something, or you want to finish a UL tage and start a new one. Now, I just want to point out what I was talking about with having a relationship between what happens in the loop and making sure that it will exit. If we were to take this count plus plus, imagine that we moved it up here. Imagine that it was inside that alt statement. Do you see what would happen? It's going to loop infinitely, because what's going to happen, it's going to get to five, and then when it gets to five, it's going to stop incrementing. It's just going to keep looping, and looping, and looping, and looping. So, it's important that we make sure that regardless of what conditional statements get executed, that we still end up with a loop that will eventually break out. Now, if you were to do this and you create an infinite loop, PHP will eventually time out.
The page will say, okay I've tried this for long enough, this seems to be an infinite loop and I'm going to give up. And that's a configurable option in PHP. Is to how long it will wait before it gives up. It is not something you typically need to modify. So, on your own,I want you to try to make a new example. And in your example, I want you to try and have a different behavior. For even numbers and odd numbers, so that as you go through a loop, and it counts up, even numbers and odd numbers are handled differently. I will give you a helpful hint, though. It might be useful for you to use the modulo operator. That's the percent sign.
And that tells you whether a number divides evenly by another number or not, by returning the remainder. So, it returns the remainder once you do division, count Modulo two tells us how many are left over if we divide count by 2. Give that a shot and make sure that you have an understanding of how while loops work. We're going to look at For loops next.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101713 Viewers
61 Video lessons · 88469 Viewers
71 Video lessons · 72295 Viewers
56 Video lessons · 104015 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.