 Hey everybody, this is Brian and welcome to the 19th Lamp Tutorial. Today, we are going to cover the for loop. In PHP, a for loop is similar to other loops that we've encountered. With a twist, of course, everything has a twist to it. We're saying for, init, condition, and increment. What in the heck is that? Now, we're saying for, meaning this is our loop, while this is true, do this. So for, init, this is a variable. We're going to say, for example, I. The condition, meaning is I, a certain value. And then we're going to increment. That's how you stay away from that dreaded infinite loop. And here's a good example down here. For, here's our initialization or init, I. I equals 1. Semi colon. Now our condition, basically what we're saying is while I is less than or equal to 5. Semi colon. And then we're going to increment I. You guessed it. This is basically a really complex version of the while and do loops. It smushes everything all into one, so you can write it all on one line. So you don't have to waste a lot of time typing. There's another variant of this called the for each loop. For each array as value. And then the code to be executed. An example of this would be you have an array, 1, 2, 3. For each X as value. Now what we're doing here is we're creating a variable called value. And I iterate over each item in the arrays. We're saying for each value in this array, do something. We're going to go over these real quick. For a loop. And we're going to say I equal 1. Semi colon. Notice the semi colon in that. It's very important. And then we're going to say while I is less than or equal to 5. And then we need our increment here. Say I plus plus. We're incrementing. That's how you keep from having an infinite loop. So this will do whatever we want it to do multiple times. So echo. And then we're just going to print out the value of I. And as you can see, 1, 2, 3, 4, 5. Nice, neat. You don't have to have your increment or decrement or worry about goofing something up. Every time it comes to this, it will increment. Once the condition is true. Pretty simple. Pretty easy to use. Much more efficient than a while in a do loop. The only downside to this is that there is no for do. Meaning it will never execute once and then evaluate the condition. That's why the do while still exists. Now we're going to do a for each. This is how typically you would go over an array. So we're going to say my array, array, 1, 2, 3. And why not? Let's just do 4. Just to be special. I'm going to say for each. Notice how the syntax for for each is a little bit different. My array. Let's jump back to W3 schools. Make sure we're doing this right here. For each variable as value. What does that as mean? Well, if you know multiple programming languages like I do, you notice that the structure of the for each is almost always identical. It's just this little guy right here always changes. I really wish they would just pick, you know, a standard. But, you know, everyone's different. Sometimes it's for each in, sometimes it's for each comma. Sometimes it's for each blah, blah, blah, as blah, blah, blah. But for PHP, it's as. And the reason why we're saying as is because we're creating a variable. Let's actually concatenate that to a little HTML goodness there. And as you can see, it does our first for loop 1, 2, 3, 4, 5. And then it comes down into our for each loop where we have an array. And this is probably the easiest way of iterating through an array. You're just basically saying for each value in this variable as whatever variable we want to assign it to, we could name this fuzzy kittens if we wanted. And then it automatically stuffs the value in that variable. So we can say, use this variable however we want because the value is already placed in their force. It loops through the array and says, OK, what index am I on this one? OK, grab that value. What index am I on this one? OK, grab that value. That's all it does. No real voodoo magic. Pretty easy. And it's probably one of the more popular used functions. Well, actually, that's pretty simple. If you've been paying attention and watching these videos so far, we're on number 19. You now have an associate's degree in PHP programming. That's right. We've covered pretty much everything you need to make some pretty complex programs. So I'm going to dub these the introduction to PHP. And as we move on, let's go out to W3 schools, I know they start forms getting post and then they say advanced is down here. But I'm going to include these in the advanced tutorials because they are a little more tricky and they require a little more knowledge. So if you've stuck with me through all of this, then you now have an associate's degree in PHP programming. You should pat yourself on the back and call up your best friend and say, hey dude, buy me a pizza. So that's all for this tutorial. Thank you for watching. I hope you found this educational and entertaining.