Welcome to part 4 of the tutorial. I’m going to talk about loops now. Loops (in PHP) are much like ‘if statements’ (see part 2), but the code written between the brackets is executed until a certain condition is met.

Let’s take a look at an example (this is called the ‘while-loop’):
<?php
$i = 2;
while($i < 60)   //The variable $i will be showed on the screen as long as $i is smaller than 60
{
echo $i.”<br>”;    //Show the number on the screen and do go down one line
$i++;   //Increase $i with 1
}
?>
You see it’s pretty easy to do a loop. If you run this script you’ll see the numbers 2 to 59 on the screen. There’s also another example I want you see:
<?php
$i = 2;
do
{
echo $i;
$i++;
}
while($i < 60)
?>
This is called the ‘do-while-loop’. It looks pretty much like the ‘while-loop’, but the difference is that this loop checks the conditions afterwards. So when you run this script, you’ll see the numbers 2 to 60 on the screen.
Another important example. The ‘for-loop’ is much like the ‘while-loop’, but the big difference is that you set the conditions and the increment of each loop. An example:
<?php
for($i = 10; $i <= 100; $i += 5)   //The variable $i is set to 10, each loop $i increases with five and the loops runs as long as $i is smaller than or equal to 100
{
echo $i.”<br>”;
}
?>
OK, that’s it. There’s nothing more to it.