logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Autism Spectrum Disorders: 4:00 PM

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

The PHP Do While Statement

Guest Author - Diane Cipollo

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!

Add The+PHP+Do+While+Statement to Twitter Add The+PHP+Do+While+Statement to Facebook Add The+PHP+Do+While+Statement to MySpace Add The+PHP+Do+While+Statement to Del.icio.us Digg The+PHP+Do+While+Statement Add The+PHP+Do+While+Statement to Yahoo My Web Add The+PHP+Do+While+Statement to Google Bookmarks Add The+PHP+Do+While+Statement to Stumbleupon Add The+PHP+Do+While+Statement to Reddit




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


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor