The Basics of PHP While Loops

Ana Kirk
What Is a PHP While Loop?

A PHP while loop is code written in the hypertext preprocessor (PHP) language to allow for the execution of instructions as long as a certain condition is true or exists. The basic order is:

1. The program looks at the condition.
2. If it finds that the condition is true, a specific set of instructions (within the loop) are followed.
3. Once the program finds that the condition is no longer true, the PHP while loop is exited.

The syntax of the while loop is the following:

while (condition to be considered by the program) {
//Follow these instructions.
}//closing curly brace

Keep in mind that the condition is enclosed within the parenthesis and the instructions to be executed within the curly braces. Remember also that if the condition is never true, the loop will never run.

When to Use a PHP While Loop

If you are starting out in web development and learning to create or work with database driven web sites, you'll find that PHP while loops are most often used when you need to retrieve records from the database that drives the site. For example, let's say you have a web site that specializes in recipes and that your home page displays the 10 latest recipes added to your site (in the database that drives the site). You could use a PHP while loop to rotate through the database, pulling the results to be displayed on your site. The loop would continue until the condition is no longer true. The limit of 10 recipes would actually be controlled in your SQL query, not your PHP code. If you are displaying results in a hypertext mark-up language (HTML) table, you'll want to be sure to keep the opening table tags and table heads as well as the closing tag outside of your loop. This is a mistake that many beginners in PHP programming make.

If you have no knowledge of SQL yet, but would still like to practice PHP while loops, you can start with a simple script that counts and then write more complicated conditions from there. All of the basic concepts of how PHP while loops work would be being gained. Let's write a loop that counts from 1 to 20. Our condition is: as long as our variable, which we'll name $i, is less than or equal to 20, we'll print the number and then increment the variable and check the condtion again. You can copy and paste the following PHP while loop code to see this in action; but, make sure you understand what's going on or you defeat the goal of learning. I've excluded the opening and closing PHP tags because they get stripped upon uploading this content. However, don't forget to include all of your code between them or it won't work.

The PHP Code With Explanation

$i = 1;
We're starting our variable out at 1. In other words, we're starting our count at 1.

while ($i <= 20) {
This is the beginning of our PHP while loop, including the condition to be checked. We're checking to see if $i is less than or equal to 20.

echo $i;
We print the result (the number).

$i++;
We increment the variable (the number) and the loop begins again by checking the condition. Use "--" to decrement avariable.

}
This is our closing curly brace.

The results will be: 1234567891011121314151617181920

The loop ceases at 20 because the condition is no longer true once $i is incremented to 21.

Source:

AAS in Web Development/PHP developer

Published by Ana Kirk

Ana Kirk is an emergency medical technician (EMT) and part-time web developer. She is also a back-up translator and author of study materials for a Christian ministry.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.