<< PHP Foreach Loop | PHP Tutorials | PHP Include and Require >>

PHP Do While Loop

The do while loop in PHP is very similar to the while loop as it will loop through a block of code until a certain condition is met. The difference is that the do while loop will execute the code before it reads the condition again. Here are two examples. The first one is a normal while loop while the second is a do while loop. Notice the difference?

<?php
$count = 0;
while ($count > 0) {
echo 'Hello there<br />';
}

echo 'Weird o_0';
?>

This is what the end users will see:

Weird o_0

Next example:

<?php
$count = 0;
do {
echo 'Hello there<br />';
} while ($count > 0);

echo 'Weird o_0';
?>

This is what the end users will see:

Hello there
Weird o_0

There is an important thing to note. The following line:

} while ($count > 0);

Must have the semicolon at the end or it will not work.

<< PHP Foreach Loop | PHP Tutorials | PHP Include and Require >>