PHP Tutorial [Part 1]

Back to Main

While Loops

While loops are easy loops to understand. They perform a loop multiple times while a some condition is true. Once it is false, it will break the loop. Here’s a simple example that does the same thing as the for loop:

<?php
$a=1;
while($a<10)
{
   echo $a."<br>";
   $a++;
}
?>

Output:

1
2
3
4
5
6
7
8
9

Just like the for loop, this one will do a loop as many times as a condition exists as true. Lets break it down: while(condition){do this over and over}. You can put what ever. You can have unrelated variables. It is a very versatile loop that can be a little easier to understand than a for loop. Again, like the example showed, it can be used in the exact same way as the for loop. It is truly all a matter of preference.

This is the end of [Part 1]. In upcoming tutorials, we will cover functions, additional loops, arrays, string, and files. If there is anything in particular you would like to see, let me know.

Pages: 1 2 3 4 5 6