Loop de loop
This is a fairly important section. Loops. Loops are present in all programming langauges their function is to make your life easier. They are basically exactly what they sound like they are. Want to do something 100 times? Loop it. We'll take a look at a few loops and why you'use them over the other. A loop will repeat a section of code that is enclosed within the loop.
Here I'm going to show you a loop and then we'll break it down.
?
for($i = 0; $i < 100; $i++) {
echo "This has been run $i times <br>";
} ?>
First, it's important to note that this is a FOR loop. Here's what it does: It assigns the variable $i as 0. Then it runs the code within the brackets. It does it for as long as $i is smaller than 100 and each time the code is run $i will be incremented by one.
It will output something like this:
This has been run 1 times
This has been run 2 times
This has been run 3 times
This has been run 4 times
This has been run 5 times
This has been run 6 times
...
This has been run 99 times
The governing body of the loop is contained within
for($i = 0; $i < 100; $i++)
The first block, $i = 0, sets the initial value.
The second block is the conditional. If this statement evaluates to true then the loop continues.
The third section is the increment. In this case $i is increased by one. It is important to note that it doesn't neccessarily have to increment.
Next let's take a look at the WHILE loop.
$i = 0;
while ($i < 100) {
//Code Goes Here
$i++;
}
You can probably figure out what this code does but just in case, let me break it down for you:
First, we begin by setting $i equal to 0, in this case our initial value. Then we enter the loop, this is fairly simple, if the conditional in the parenthesis evaluate to true then the loop is run. This loop doesn't have a built in method of incrementing, thus we have the $i++. We use this to increase the value of $i by one and, as with the last example, once $i reaches 100 the loop will terminate.
Putting it all together
So, you've gone through the first sections of this guide. Congratulations! You've learned all the basic functions to making a PHP application!