Looping Statement In PHP
What is Looping Statement In PHP?
Loops execute a block of code a specified number of times, or while a specified condition is true.
Type of Loops in PHP
- The while Loop
- The Do …while Loop
- The For Loop
- The Foreach Loop
- Break and Continue Statement
The While Loop
While structure is the another type of loop statement, where the condition is checked at first, the iteration will not stop even if the value changes while executing Statements.
Syntax-
1 2 3 4 |
while(condition) { code to be executed } |
Example-
1 2 3 4 5 6 7 8 |
<?php $i=0; while($i<=10) { //output value 0 from 10 echo "The Number is ".$i."<br>"; $i++; } ?> |
Output-
The Number is 0
The Number is 1
The Number is 2
The Number is 3
The Number is 4
The Number is 5
The Number is 6
The Number is 7
The Number is 8
The Number is 9
The Number is 10
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.
Syntax –
1 2 3 4 5 |
do { code to be executed } while(condition); |
Example –
1 2 3 4 5 6 7 8 9 |
<?php $i=0; do { //output value 0 from 10 echo "The Number is ".$i."<br>"; $i++; } while($i<=10) ?> |
Output-
The Number is 0
The Number is 1
The Number is 2
The Number is 3
The Number is 4
The Number is 5
The Number is 6
The Number is 7
The Number is 8
The Number is 9
The Number is 10
The For Loop
The for loop is used when you know in advance how many times the script should run.
Syntax-
1 2 3 4 |
for(initialization;condition;increment) { code to be executed } |
The for loop statement take three expressions inside its parenthesis. Separated by semi-colons. When the for loop executes, the following occurs –
- The initialization expression is executed. This expression usually initializes one or more loop counter, 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 statement execute. If the value of condition is false, the for loop terminates.
- The update expression increment executes.
- The statement executes, and control return to step 2.
Example –
1 2 3 4 5 6 |
<?php for($i=0;$i<=10;$i++) { echo "The Number is ".$i."<br/>"; } ?> |
Output-
The Number is 0
The Number is 1
The Number is 2
The Number is 3
The Number is 4
The Number is 5
The Number is 6
The Number is 7
The Number is 8
The Number is 9
The Number is 10
The Foreach Loop
For Each structure is a loop structure used for arrays.
Syntax –
1 2 3 4 5 6 7 8 |
foreach(array as value) { code to be executed; } foreach(array as key => value) { code to be executed; } |
Example-
1 2 3 4 5 6 7 |
<?php $email=array('info@phpgurukul.com','anuj.lpu1@gmail.com'); foreach($email as $value) { echo "Processing ".$value."<br/>"; } ?> |
Output-
Processing info@phpgurukul.com
Processing anuj.lpu1@gmail.com
The Break Statement
Break end the execution of the for , for each , do-while or switch statement.
Syntax-
1 |
break(optional numeric argument) |
Example-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $c=0; while(++$c) { switch($c) { case 5 : echo "At 5;<br />"; break 1; case 10 ; echo "At 10;quitting<br />"; break 2; default : break; } } ?> |
Output-
At 5;
At 10;quitting
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 exist from the whole loop.
Syntax –
1 |
continue(optional numeric argument); |
Example –
1 2 3 4 5 6 7 8 9 10 |
<?php for($c=0;$c<=10;++$c) { if($c==5) { continue; } print "$c n"; } ?> |
Output – 0 1 2 3 4 6 7 8 9 10