PDA

View Full Version : Basic PHP Tutorial - 4



ctt1010
11-01-2014, 02:02 PM
In this tutorial, we are going to look at a "while" loop. This loop executes a block of code as long as the specified condition is true. We could use this for counting so it does X 5 times etc.

A while loop looks like this :



<?php
$x=0;

while($x<6) {
echo $x;
$x++;

}
?>


In here, a variable of x is assigned with the value 0. The while look says : while the variable x ($x) is less than 5 (<5)

Echo (print) out the variable.
Add 1 to the x variable.

So what you should get printed out is :


1
2
3
4
5

This is useful for a number of things in PHP.