FOR Loop

Hey guys, in this video we will be looking at the for loop in PHP. First thing I want to do is create the file for the for loop in our already established folder for this website. So I’ll just go ahead and click new file and write for loop dot php. Next, I will generate my html5 template and then go ahead and set the title to be PHP primer. I fn for loop. Now conceptually a for loop is what we call a counter controlled loop, meaning it does something for a certain and a set number of times it’s usually used when you know exactly how many times something needs to happen. And so you use a for loop to repeat it for that exact number of times as long as you get your parameters right. Alright, so this construct is available in any program. language. And it is pretty cool in PHP because it allows you to kind of dynamically generate things based on certain variables. Now before we can write any PHP code, we know that we should put in a header tag to see which page we’re on. So I’m just borrowing this from the switch statements page. And I’m just going to write an h1 tag at the top of the page to see that we’re on the for loops page. All right, next up, we’re going to initialize our PHP tags. And inside our tags is where we will be able to read or PHP, so I’m just going to add a comment and remember comments don’t get executed. That’s why they’re handy. They’re just there for documentation purposes so that when you look at this code file, you have an idea of what you know, comes afterwards. So it’s always a good idea to write comments in your code. That’s only for other people who might read your code book. also play yourself because you may have taken a break, and then come back and you need to pick up where you left off. So comment always helps with that. Now the syntax for a for loop is you write the word for, then you open and close your braces, your parentheses. And then inside of these parentheses, you declare a variable. So I’m going to declare a variable and I’m going to call it call went, alright. And well, based on how I named the variable, you could call this variable anything but I’m naming a colon because that’s exactly what we wanted to do, we wanted to keep count of the number of times this query will execute. All right, and then I’m going to have a condition meaning under what condition should I stop executing. So here, I can see that the condition under which I should stop repeating a certain action or this loop should stop repeating that particular action is I wanted to under the condition that count is less than, let’s see 10. All right, so this should run for 10 times Why? Because it’s starting off with a value of zero. And then it should change to one, then two, then three, then four, then five. So once it hits the condition where code is no longer less than 10, meaning it’s, when it’s nine, it will run down. And when this comes up to be 10, then 10 is no longer less than 10, then it will x exit that loop. All right, so if you want it to run for 11 times or for 20 times, you would say it is less than 20. All right, and be sure that you’re setting this to zero. If you set this to one. I mean, it will still work, but then you’ll have to be keen to know that if it starts off at one then you’re missing one iteration because when it starts at zero, it does an iteration With a value of zero, then it goes to one, then two, then three. So if you really want it to run for 10 or 20 times, then you need to start at zero. And then you can see run until you are less than 10. So once it violates the fact that’s less than 10, it will exit. And then after that, what we need to do is to let it know that it needs to count. So we just write the variable and then we say plus plus. Alright, so plus plus is a post incremental, which means that it will run with the value times No, and then when the loop is finished doing its thing, it comes back then it says, okay, check the condition. I’m still zero. Okay, that’s cool. Colin’s up to one. All right. Well, it actually says count up to one and then check if you’re if the condition is still part. So that’s what happens right there. Now moving on. The next thing we need is our curly braces. So you’ll see this curly brace appearing a lot in PHP. In other programming languages, so just get used to it that once you have all constructs that it’s an if statements or switch statements a for loop while loop and many other constructs, you just have to put your code for that construct inside of open and close curly braces. Now what if we wanted to print to the screen 10 times Hi, hello world. So I’m going to write echo. And I’m going to print and I’m going to put this in p tag this time. And I’m going to say hello, world and then close the p tag. So I want this to print for 10 times the text hello world. So that for loop is going to start off at zero and run 10 times and each time it runs it’s going to do whatever is inside the body to get done. Now I’m going back to my homepage and then I’m going to click on page for for loop. And then we see here that we have 10 paragraph tags for the text hello world. And we didn’t have to do anything. We just wrote a for loop and we wrote Hello or hello world once, and we got 10 lines. And that’s the power of a loop. Because then we can dynamically generate HTML tags or whatever contents we need as many times as we need it on that particular page. Now let’s try something else. What if I wanted to print the value of con so I’m going to do another example. So I’m just going to rewrite this for loop. And I’m going to do it from scratch for and please note that the variable that you declare in this is within what we call the scope. So if you would call a variable Collins up here, call it only exists for as long as this this loop runs. Once it exits that loop, meaning it’s finished with repeating itself that Variable no longer exists. Alright, so I’m going to reuse that variable because I know I won’t have any conflict because it stopped existing by it. By the time it hit this next loop, and I’m going to initialize it to zero, I’m going to do the same condition. And I am going to count plus plus. And then what I want to do in this for loop is actually print the value each time it is run. So I want to print to the screen or to the extent of the page well to the browser, the value of count and accomplish that we know that echo is what we use to print and I’m going to use interpolation. So I’m going to use my double quotation marks. And I’m just ending my lines I don’t forget to do it later. And I’m going to say V combed his colon, and then I’m just going to reference my variable called cones. So that interpolation at work we looked at concatenation earlier. And we realize that while it can work with the double quotes, it’s absolutely necessary with a single quotes. And if we’re using the double quotes, we don’t have to have to use concatenation. Of course, this depends on your version of PHP. So if it doesn’t work, then you can upgrade or if you will, for upgrading is not an option. You can always concatenate. Now let’s see what this does. So when I refresh the page, I should see Hello World 10 times. Yes, and I should also see our countdown, our cones up for the text. So you’ll see here it’s echoing the columns is zero, the colons is one but then it’s all in one line because in a previous video, we would have established that we probably want to break line between our echo statements. So firstly, I’m going to put this so what happens is that when you print the static text like that, it will all print in one line. All right. But then, naturally in HTML, what happens is that a p tag comes with its own what we call a padding. So it has its own spacing from the top and the bottom. So the P tags wouldn’t come in one line. So look at the difference. We are echoing p tags, hello world, they all come in their own line. When we echo the static text, it all comes in one line. So to remedy that, we could echo a break tag, or we could just put it in the appropriate type. Because really, actually, you don’t want to be printing the static text to an HTML page, I always recommend that you anything you do, you put it inside of some form of tag so that the browser can handle it a certain way. Alright, so when I refresh this, then the behavior between the hello world and the counting is the same because they’re both p tags. And further to that, we also see that we are printing all the code so we see where it starts with one with zero goes to one through nine, there is no 10 because once again once it hits the value 10 and 10 is no longer less than 10 then it will exit that loop. And that’s pretty much it for the for loop. Go ahead and experiment, see what you can generate for as many times as you need to generate it on the web page, add some comments and share this code file with you and see you later.