Tutorial 22: Do While Loop


The Do While Loop is a bit like the While Loop, but there's one major difference: the While Loop will only start if the condition is TRUE; whereas the Do While Loop will always execute the first time, and then evaluate whether the condition is TRUE afterwards.

Here's what the syntax looks like:

do {
	
	// execute code
	
} while ( condition to be met );

Let's look at a real example:

// Set a variable
$i = 1;

do {
	// Do this code
	echo "Number: $i 
"; $i++; } // Then evaluate this condition // Repeat loop if TRUE while ( $i <= 10 );

If coded correctly, this should echo the numbers 1 to 10 on your screen.

Feel free to experiment with the above Do While Loop with some PHP using the practice.php page provided in this folder.

Check out the final example
©2019 Brad Hussey