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. In PHP we have the following looping statements: while - loops through a block of code if and as long as a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a special 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 Statement The while statement will execute a block of code if and as long as a condition is true. Syntax Code: while (condition) { code to be executed; } Example The following example demonstrates a loop that will continue to run as long as the variable 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> The do...while Statement The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. Syntax Code: do { code to be executed; } while (condition); Example The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 5: Code: <html> <body> <?php $i=0; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<5); ?> </body> </html> The for Statement The for statement is used when you know how many times you want to execute a statement or a list of statements. Syntax Code: for (initialization; condition; increment) { code to be executed; } The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false. Example The following example prints the text "Hello World!" five times: Code: <html> <body> <?php for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> </body> </html> The foreach Statement The foreach statement is used to loop through arrays. For every loop, the value of the current array element is assigned to $value so on the next loop. Syntax Code: foreach (array as value) { code to be executed; } Example The following example demonstrates a loop that will print the values of the given array: Code: <html> <body> <?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html>
Hi thanks for the article. I m a beginner in PHP and now i m in looping chapter. I think i will facing some prob with this can you sort out this. thanks
In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. PHP has two major groups of looping statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements. exemples:- The While Loop Code: while (condition) { code needed to loop; } The Do...While Loop Code: do { code needed to loop; } while (condition); The For Loop Code: for (initialization; condition; increment) { code needed to loop; } The Foreach Loop Code: foreach (array as value) { code needed to loop; } Break and Continue Statements PHP: <?php for ($i = 0; $i <= 10; $i++) { if($i==5) { break; } echo "The number is " . $i . "<br/>"; } ?>
Sequence of instructions in an infinite loop the loop endlessly computer program, either due to cyclic non-termination conditions, one can never meet, or to cycle to start again. In the old cooperative multitasking operating system, an infinite loop is usually cause the entire system to stop responding. And now, the popular model of preemptive multitasking, infinite loops usually cause the program consumes all available processor time, but usually can be terminated by the user. Busy wait loop is sometimes mistakenly called "infinite loop. " One possible reason for a computer "freeze"is an infinite loop, the other including the deadlock and an access violation.
The common tasks that are covered by a for loop are: 1. Set a counter variable to some initial value. 2. Check to see if the conditional statement is true. 3. Execute the code within the loop. 4. Increment a counter at the end of each iteration through the loop. The for loop allows you to define these steps in one easy line of code. It may seem to have a strange form, so pay close attention to the syntax used! Let us take the example from the while loop lesson and see how it could be done in a for loop. The basic structure of the for loop is as follows: Pseudo PHP Code: for ( initialize a counter; conditional statement; increment a counter) { do this code; } Notice how all the steps of the loop are taken care of in the for loop statement. Each step is separated by a semicolon: initiliaze counter, conditional statement, and the counter increment. A semicolon is needed because these are separate expressions. However, notice that a semicolon is not needed after the "increment counter" expression.