The PHP Do While Statement

The PHP Do While Statement
This tutorial is about the PHP do..while statement which is a loop statement similar to the while statement that we discussed in the previous tutorial. And like the while statement, you can use the do..while statement to instruct your program to perform a task repeatedly until you tell it to stop. It will stop when the test expression no longer evaluates as true. Here is the code.

do { do this }
while (expression);
$number = 1;

do { echo "$number <br>";
$number++;
}
while ($number < 11);



$number = 1;
If you have read the previous tutorial about the while statement, you will remember that we must first initialize the test variable $number to a beginning value. This step is not part of the loop statement and may not be necessary if the test variable has already been initialized earlier in the program. But for our example, the test variable has not been initialized previously.

do { echo "$number <br>";
$number++;
}
do { do this }
This code will be repeated continuously until you tell it to stop in the second part of the do..while statement. The example code will print the current value of $number and then the HTML <br> code to the web browser. Next it will increment the value of $number by 1. This second part of the code must be included to change the value of $number each time the loop is executed. If it is missing the value of $number will never change and the loop will be repeated indefinitely. The code $number++; will increment the value of $number by one each time through the loop and the program will stop the loop when the value is 11.

while ($number < 11);
while (expression);
This is the code that controls the number of times the loop is executed. In the example the loop will stop when the text expression is no longer true (the value of the test variable is no longer less than 11). Also notice that the statement ends with a semicolon.

If you compare the while statement to the do..while statement you will notice that the code to be executed do { do this} and the code that controls the loop while (expression); are switched. So what does that mean? It means that the do { do this} code will always be executed at least once in the do..while statement because the controlling code doesn't evaluate the test expression until after the do { do this} code is performed for the first time. However, in the while statement the test expression is evaluated first and it is possible that the do { do this } will never be executed. Therefore you would want to use the do..while statement when you want the do { do this} part of the code to be performed at least once.

The result will look like this in the web browser.

1
2
3
4
5
6
7
8
9
10






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.