2. Looping statement Loops execute a block of code a specified number of times, or while a specified condition is true. In PHP, the following looping statements are used: The While Loop The Do...While Loop The For Loop The Foreach Loop Break and Continue Statements 2 www.YouStudy.IN
3. The while loop While structure is another type of loop statements, where the condition is checked at first, the iteration will not stop even if the value changes while executing statements. 3 www.YouStudy.IN
4. Syntax while (condition) { code to be executed;} 4 www.YouStudy.IN
5. Example <?php$i=0;while ($i <= 10) { // Output values from 0 to 10 echo "The number is ".$i."<br />"; $i++;}?> 5 www.YouStudy.IN
6. The do while loop Do While statement is same as the while statement, the only difference is that it evaluates the expression at the end. 6 www.YouStudy.IN
7. Syntax do { code to be exected;}while (condition); 7 www.YouStudy.IN
8. Example <?php $i = 0;do { echo "The number is ".$i."<br/>"; $i++;} while ($i <= 10);?> 8 www.YouStudy.IN
9. The For Loop The for loop is used when you know in advance how many times the script should run. 9 www.YouStudy.IN
10. Syntax for (initialization; condition; increment) { code to be executed;} 10 www.YouStudy.IN
11. For loop cont.. he For statement takes three expressions inside its parentheses, separated by semi-colons. When the For loop executes, the following occurs: The initializing expression is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the For loop terminates. The update expression increment executes. The statements execute, and control returns to step 2. 11 www.YouStudy.IN
12. Example <?phpfor ($i=0; $i <= 10; $i++) { echo "The number is ".$i."<br />";}?> 12 www.YouStudy.IN
13. The Foreach Loop For Each structure is a loop structure used for arrays 13 www.YouStudy.IN
14. Syntax foreach (array as value){ code to be executed;} foreach (array as key => value){ code to be executed;} 14 www.YouStudy.IN
15. Example <?php$email = array('john.smith@example.com', 'alex@example.com');foreach ($email as $value) { echo "Processing ".$value."<br />";}?> 15 www.YouStudy.IN
16. The break statement Break ends the execution of the for, for each, while, do-while or switch statement. 16 www.YouStudy.IN
18. Example <?php $c = 0; while (++$c) { switch ($c) { case 5: echo "At 5;"; break 1; case 10: echo "At 10; quitting"; break 2; default: break; } } ?> 18 www.YouStudy.IN
19. The continue statement "Continue" is used to skip the current loop iteration and continue with the next iteration of the loop. But "Break" is used to exit from the whole loop. 19 www.YouStudy.IN