Loops in PHP| While in PHP| do while PHP| foreach in PHP
PHP do while loop
In do while loop condition is always checked the execution of code. So, do while will run at least one time always rest it depends on the code. The do-while loop is very much similar to the while loop except for the condition check. The main difference between both loops is that while loop checks the condition at the beginning, whereas do-while loop checks the condition at the end of the loop.
Syntax
do{
//code to be executed
}while(condition);
Example:
<?php $n = 1; do { echo "$n<br/>"; $n++; } while ($n <= 10); ?>
The output will be:
1
2
3
4
5
6
7
8
9
10
A semicolon is used to terminate the do-while loop.
<?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?>
Working on the program
- The first variable $x will be initialized from 1. Then the program will go to the body of do loop and will print 1 as a value of $x.
- Then it will go to $x++; to increase the value by 1. (so here it prints the value first, then increase the value, then check the condition if it is true or not)
- Now after increment, the program will go to the while statement to check the condition. As 1 is less than or equals to 5, it will go back to echo statement to print value 2
- Then it will come down and increase the value to 3 and repeat itself till value becomes equals to 5 and print all the numbers till 5.
The output will be:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Difference between while loop and do-while loop
While loop | Do while loop |
While loop is an entry-controlled loop which means it tests the condition first then run the program | Do while loop is exit controlled loop which means it runs the program first then tests the condition. |
While loop can be run 0 or more than 0 times | Do while loop runs at least once because it first executes its command then check the condition |
The variables must be initialized first | It is not necessary to initialize the variables to test the condition |
While (condition) { Statements } | Do { Statements } while (condition) |
PHP foreach loop
For each loop runs its statements for each element in an array
Syntax
foreach ($array as $value) {
code to be executed;
}
Example: this will print each element of an array
<?php $month = ["Jan", "Feb", "March"]; foreach ($month as $m) { echo "Month is: $m<br />"; } ?>
The output will be:
Month is: Jan
Month is: Feb
Month is: March
PHP break statement
PHP break statement ends the current execution of for, foreach, while, do while loop. In other words, it will break the execution of the code when it reaches its specified condition in the if statement.
Example:
<?php for ($i = 1; $i <= 10; $i++) { // code... if ($i == 5) { // code... break; } echo $i; echo "<br>"; } ?>
Working of break statement:
- First initialization starts from 1. Then the condition is checked. Here 1 is less than or equals 10. So, it reaches to if condition where the code has to check if 1 is equal to 5. Here the value 1 is not equal to 5 so the program will move down to print value 1.
- Then it will go back to increment the number. Again, it will come down to if condition. This cycle will keep on repeating until the value 5 goes to if condition and matches the condition.
- When value 5 matches to if condition i.e. if (5<=5), the program will stop its execution and terminate the code without going further to increment operator.
- So, this is the whole process of a break statement.
PHP Continue
PHP continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
Example:
<?php for ($i = 1; $i <= 10; $i++) { if ($i == 5) { continue; } echo $i; echo "<br>"; } ?>
The output will be:
1
2
3
4
6
7
8
9
10
Working on the program
- First initialization starts from 1. Then the condition is checked. Here 1 is less than or equals 10. So, it reaches to if condition where the code has to check if 1 is equal to 5. Here the value 1 is not equal to 5 so the program will move down to print value 1.
- Then it will go back to increment the number. Again, it will come down to if condition. This cycle will keep on repeating until the value 5 goes to if condition and matches the condition.
- When value 5 matches to if condition i.e. if (5<=5), the program will skip the number 5 and will again go back to increment the number to 6
- So, it will keep on repeating until the program reaches 10 numbers by skipping number 5 and will terminate in the end by itself.