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
Coping with Dementia Part 4: "We're All Mad Here"Final in a 4-part series on how dementia, like a thief in the night, stole bits of my mother-in-law's mind. Our journey from onset (1), retirement community (2), living w/ us (3...- Basics of Web DevelopmentIn this guide, I will briefly go through the main language and concepts you will need to know to be a real web developer.
- Important Steps Before You Hire a PHP Web Development ServiceThere is no doubt that PHP has made a place amongst the popular programming languages, and the with the boom in the web designing and development industry, more and more web development companies are going for the web...
- How to Choose a Web Development CompanyToday, where there is a grandeur of talents in this industry, and in this cat-and-dog race of of web designers and web developers, you need to be well equipped with some of the pointers you ought to keep in mind to av...
Assessing Today's Musical Talent: Are Great Singers Born with It?When we're talking about truly great singers, there's no question that these people are born with their talent. Artists like Mariah Carey and Celine Dion clearly have be...
- How to Produce Good and Resource-friendly PHP Code
- The Basics of PHP Sessions
- Introduction to PHP for Beginners Part 2
- PHP File Handling Basics
- PHP Error Basics
- PHP Loop Statements
- Basic Beginner Scrap Crochet Pattern: Funky Bangle Bracelet




