Code Snippet: While loops (PHP)

No Comments

If you want to organise or display data on a website, the best way is the php function ‘while’. Example of usage:

$i = 0;

while($i <= 6) {

echo $i;

$i++;

}

This code prints out the numbers 0 through 6 (0123456). Advancing this, you could have an array with 3 values in, and call them up like this:

$array = array('one', 'two', 'three');

$i = 0; // Remember Arrays start at 0

while($i <= 2) { // After I reaches 2, this loop stops - that gives the three values

echo $array[$i];

echo "<br>"; // html code for line break

$i++;

}

This code gives the following output:

one

two

three

I’ll advance on this later, but for now – it’s late.

Leave a Reply