0% found this document useful (0 votes)
12 views

PHP Tizag Tutorial-42

A while loop in PHP allows code to be repeatedly executed as long as a specified condition is true. It first checks the condition, executes the code if true, then loops back to re-check the condition. This allows repetitive tasks like printing out the costs of different brush quantities for a store price chart to be automated rather than done manually. The basic structure of a PHP while loop is to specify a condition, put the code to be repeated inside curly braces, and it will loop as long as the condition remains true.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PHP Tizag Tutorial-42

A while loop in PHP allows code to be repeatedly executed as long as a specified condition is true. It first checks the condition, executes the code if true, then loops back to re-check the condition. This allows repetitive tasks like printing out the costs of different brush quantities for a store price chart to be automated rather than done manually. The basic structure of a PHP while loop is to specify a condition, put the code to be repeated inside curly braces, and it will loop as long as the condition remains true.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - While Loop

Repetitive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work
are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such
repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.
The idea of a loop is to do something over and over again until the task has been completed. Before we
show a real example of when you might need one, let's go over the structure of the PHP while loop.

Simple While Loop Example

The function of the while loop is to do a task over and over as long as the specified conditional statement
is true. This logical check is the same as the one that appears in a PHP if statement to determine if it is true or
false. Here is the basic structure of a PHP while loop:

Pseudo PHP Code:


while ( conditional statement is true){
//do this code;
}

This isn't valid PHP code, but it displays how the while loop is structured. Here is the break down of how a
while loop functions when your script is executing:

1. The conditional statement is checked. If it is true, then (2) occurs. If it is false, then (4) occurs.
2. The code within the while loop is executed.
3. The process starts again at (1). Effectively "looping" back.
4. If the conditional statement is false, then the code within is not executed and there is no more looping.
The code following the while loop is then executed like normal.

A Real While Loop Example

Imagine that you are running an art supply store. You would like to print out the price chart for number of
brushes and total cost. You sell brushes at a flat rate, but would like to display how much different quantities
would cost. This will save your customers from having to do the mental math themselves.
You know that a while loop would be perfect for this repetitive and boring task. Here is how to go about
doing it.

You might also like