www.pickatutorial.com Computer Tutorials
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP do...while Loop Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Just like anyother programming language looping statements in PHP are also used to execute the same block of code a specified number of times. Often when we write a program we need some block of code to run a number of times. We can use looping statements in our code to perform this.

In PHP we have the following looping constructs available:

while - loops through a block of code as long as a specified condition is true.
do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true.
for - loops through a block of code a specified number of times.
foreach - loops through a block of code for each element in an array.

The while Loop
The while statement executes a block of code as long as a specified condition is true. Its syntax follows:

while (condition)
code to be executed;


Example
The following example demonstrates a loop that continues to run as long as the variable i is less than, or equal to 10. Note that the variable i is incremented by 1 on each iteration of the while loop:

<html>
<body>
<?php
$i=1;
while($i<=10)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>


The do...while Statement
The do...while statement in PHP is very similar to the while statement. There is just one difference being that the do...while loop is guaranteed to execute at least once no matter the loop condition is true or false. This is because in case of a while loop the condition is evaluated before every iteration, on the contrary in the case of a do...while loop the loop condition is evaluated after every iteration. The syntax of a do...while loop follows:

do
{
code to be executed;
}
while (condition);

Next: PHP For Loop


Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.