PHP Looping - While Loop Loops execute a block of code a specified number of times, or while a specified condition is true. 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. Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this. In PHP, we have the following looping statements: - while - loops through a block of code while 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 executes a block of code while a condition is true. 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: Syntax: while (condition) { code to be executed; } 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: The conditional statement is checked. If it is true, then (2) occurs. If it is false, then (4) occurs. The code within the while loop is executed. The process starts again at (1). Effectively "looping" back. 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. Example: The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: Code: <html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html> An output will be: The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 A "do while" loop is a slightly modified version of the while loop. If you recal from one of the previous lessons on While Loops the conditional statement is checked comes back true then the code within the while loop is executed. If the conditional statement is false then the code within the loop is not executed. The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. Note: The conditional statement is not checked until after the contained code has been executed. Syntax: do { code to be executed; } while (condition); Example: The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5: Code: <html> <body> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> </body> </html> An output of the above code will be: The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 *Chances are you will not need to use a do while loop in most of your PHP programming, but it is good to know it's there! PHP FOR LOOP
|