PHP loops let you run a block of PHP code over and over again. They’re great for repetitive tasks such as reading data records from a database, or displaying rows of a table in a Web page.
The PHP while
loop is the simplest type of loop to understand. In this article you look at the syntax of a PHP while
loop, and explore some example loops. You also look at do...while
loops.
What is a while
loop?
A while
loop is a construct that lets you run a block of code repeatedly as long as a certain condition is true
. As soon as the condition becomes false
, the loop exits.
Here’s the general syntax of a PHP while
loop:
while ( condition ) {
// This code is run as long as condition is true
}
// This code is run after the loop finishes
Here’s how it works:
- The PHP engine tests the
condition
inside the parentheses. - If the condition is
true
then the PHP engine runs the code inside the braces ({}
), then retests the condition. If it’s stilltrue
then the code inside the braces is run again, and so on. - If the condition is
false
then the PHP engine exits the loop, and starts running any code after the closing brace.
Typically, you write the condition in such a way that it is true
while you want your loop to run, and becomes false
when you want your loop to exit. For example, if your loop should count to ten before exiting then the condition might be:
while ( $counter <= 10 ) {
...
A PHP while
loop example
Here’s a simple example script that uses a while
loop to count from 1 to 10:
$counter = 1;
while ( $counter <= 10 ) {
echo "I've counted to $counter<br />";
$counter++;
}
echo "All done!";
When run, this code displays the following in a Web browser:
I've counted to 1 I've counted to 2 I've counted to 3 I've counted to 4 I've counted to 5 I've counted to 6 I've counted to 7 I've counted to 8 I've counted to 9 I've counted to 10 All done!
The script first sets a $counter
variable to 1. Then it starts looping. Each time through the loop, $counter
is tested to make sure it’s still less than or equal to 10; as long as it is, the loop repeats. Inside the loop, the current value of $counter
is displayed, then $counter
is incremented by 1.
Once $counter
reaches 11, the condition in the while
loop becomes false
and the loop exits, displaying the “All done!” message.
The PHP do...while
loop
The do...while
loop is a variation of the while
loop. The only difference is that the expression is tested after the code block has been run, rather than before:
do {
// This code is run at least once
} while ( condition )
// This code is run after the loop finishes
Here’s how a do...while
loop runs:
- The PHP engine first runs the code block inside the braces (
{}
). - Next, the PHP engine tests the
condition
inside the parentheses. - If the condition is
true
then the code inside the braces is run again, and so on. - If the condition is
false
then the PHP engine exits the loop, and starts running any code after the closing brace.
You can see that, with a do...while
loop, the code inside the loop is always run at least once, even if the condition is false
to start with. This can be useful in certain situations. For example, if your condition includes a variable that is set inside the loop then you need to run the loop at least once before you can test the condition.
Here’s an example of a do...while
loop — a simple number-guessing game:
$theNumber = rand ( 1, 10 );
do {
$myGuess = rand ( 1, 10 );
echo "I'm guessing: $myGuess</br />";
} while ( $myGuess != $theNumber );
echo "I got it! It's $myGuess!<br />";
The script displays something like this:
I'm guessing: 3 I'm guessing: 5 I'm guessing: 7 I'm guessing: 2 I got it! It's 2!
First, the script sets a new variable, $theNumber
, to a random number between 1 and 10. Then it begins the do...while
loop. Within the loop, the script sets another variable, $myGuess
, to another random number between 1 and 10, and displays the guess.
Next, the loop condition tests to see if $theNumber
and $myGuess
are different; if they are then the loop runs again so that another guess can be made. This repeats until $myGuess
equals $theNumber
; at this point the loop exits and the script displays a success message.
You’ve now seen how to use PHP’s while
and do...while
loops to write code that runs repeatedly as long as a condition is met. Loops are simple yet powerful constructs that you’ll use often in your PHP scripts. Happy coding!
Leave a Reply