So when we're dealing with loops, what we're doing is we're running the same code multiple times.
Ah, and you do this for a number of different reasons.
You may do this if you're going to be seen, what the total cost will be on something like interest if you take out alone.
So you could say if you take out $100 loan with 5% interest than the total that you'll O in the first year will be, ah, $105.
And then what you can do with a loop is you can say then in the second year and be out on $105 plus 5% whatever that is in the second year, the third year, the fourth year of the fifth year, that type of thing.
So if you're looking for things like if you're looking to track things longitude early, or be able to show that that's one of reasons that you would use a loop or another reason that you would use a loop is if you're going to be doing a report out of something like a database or a file.
So if you're going to be pulled pulling multiple records out of a database or file and then you're going to printing that out, what you would dio is, basically, you would say, Print this information for the first line from a file and then loop.
Continue to do that until you get to the end of the file.
So loops air just a simple way to do a repetitive task until a certain condition ends up being true.
Many times, people decide to use a four loops of four.
Loop is one of the standard loops.
There's also a do, and they do wild loop.
But a lot of people, especially for simple tasks, to decide that using a four loop is the best way to go, so that let's go over the computer so I can show you what a four loop looks like.
So this is a simple script to show you how the four loops works.
I've called this four dot PHP and as always, open this with the PHP tag.
And so this right here really is all there is to a four loop.
So the first thing that we're going to say is four f o.
After that, we're going to open a parentheses, E, and then we're going to give this four loop three different arguments.
So the first thing that we're going to say is we're going to for this particular script is we're going to create a variable called X, and we're going to set the value of that variable to one.
So we're going to create variable dollar side X, and we're going to set the value to one.
We're then going to do a semi colon.
Remember, basically, any time that you kind of think that, like a line of code should end a lot of times you'll use a semi colon.
So you put a semicolon here where we go to the next argument.
Then what we're going to say is for that variable that we just created Dollar side X, while dollar Sign X is less than or equal to 20.
We want this loop to continue, so we're going to set.
We're going to create and set the value of variable X to one, and then while X is less than or equal to 20 we want this script to continue and in the final argument here again, after a semi colon is, we're going to say dollar sign X plus plus.
So what this does is this takes the value of X.
It adds one to the value of X and then sets the value of X to that new number.
So here, so initially, X equals one.
So what X Plus dollar sign?
X Plus plus does is it says X plus one essentially sets to X.
So one plus one is now the value of dollar sign X.
So now it's two than X plus, plus the next iteration it will be two plus one.
So then dollar sign X will be worth three and then four and then five and then six.
So this is how you do the adoration.
So we create X.
We set it to one, while X is less than or equal to 20.
Keep doing the loop at the end of each loop.
What we want you to do is you want you to add one to the value of X, and that's what we'll increment it up.
Then all we're doing here, we've been closed the parentheses.
We open a squirrely bracket And then the first thing that we're going to do is we're going to print and then with this is we print the value for X.
So the first time this goes around will be one than to the three of the four.
The five and six, Basically, whatever the current value of X is, this is what it will print course.
We end with a semi colon, then to make this something relatively easy to read and a Web browser.
Well, then also do prints.
Double quotation marks will do break.
So this is just a simple HTML break.
Close double quotation marks against semi colon.
We will close the squiggly bracket, so this closes out what the while loop is supposed to do.
So again, You could have one line here or 1000 lines here.
Basically, anything between the two squiggly brackets will run for each generation a loop.
Then, of course, what we're going to do is we're going to close PHP, and this is what our loop looks like.
So then, with that, we go over to our Web browser.
We've type in silicon does oh dot com We do four dot PHP and this is what it looks like.
So if we look at here so initially, X equals one and then print acts break.
So a prince one.
Then when that loop is done, X plus plus X is now worth to the next is worth +345 and it keeps going all the way until X is either less than or equal to 20.
And that's why we get up to 20.
So if we go here and we do something such as, we just set this to 10 so now we're going to set it initially to 10.
We do save you, go over, we upload.
Now when we go back to Google Chrome and we do a refresh, then we can see ex starts at 10 because we now have X starting a 10 and then it continues until it hits 20.
So this is basically how that four loop works Now it is important, understand that the variable does not have to be created and actually set within the four loop itself.
We could just simply call the variable X, but then set it outside so we could have we could create X said it to, you know, five up here said it to a value.
And this is another way that we could run the four loop.
So this this is a way that let's say, if you're grabbing information from a form or you're grabbing information from something else, this is a way you can create and set the value of the variable from the output of something, whether it's a database, whether it's a form, whether it's a file, then once this has been created and set, then it could be given to this loop.
So with this, this will run basically the same way we click save we d'oh!
And then if we were fresh, we can see now it starts at five.
So, ex we create the variable X, we said at the five, and then here all we do is we just plug in X without anything, and then we have the same loop going on.
So this is basically how the four loop looks and how it works in the real world.
So that's all there is the four loops and PHP and four loops or one of the standard ways that you do loops and PHP.
Now, if you haven't been dealing with files or databases yet.
This may not seem that impressive.
Obviously, you can use this for some You know, some fancy mathematical tricks, that kind of thing again, if you're looking at interest rates or how long it will take you to pay down alone or something along those lines.
But where this really becomes valuable is when you start reading to files or when you start inputting data in two files or databases, you can use something like a four loop to pull information out, depending on different variables.
So that's what a four loop is, and that's why they matter.