PHP Loops
PHP Loops learns us how to repeat code by using loops in PHP.
PHP Loops
When you write code often, you want the same block of code to run over and over again a certain number of times. So, for adding several number of codes we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
Loop types:
- while - loops through a block of code as long as the specified condition is true
- do...while - loops through a block of code once, and then repeats the loop as long as the 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 PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
The while loop is also an entry control loop like for loops i.e., it first checks the condition at the start of the loop and if its true then it enters the loop and executes the block of statements, and goes on executing it as long as the condition holds true.
while (condition is true)
{
code to be executed;
}
The Example below displays the numbers from 11 to 20:
while(condition): //code to be executed endwhile;
Nested While Loop
We can use while loop inside another while loop in PHP, it is known as nested while loop.
In case of inner or nested while loop, nested while loop is executed fully for one outer while loop. If outer while loop is to be executed for 3 times and nested while loop for 3 times, nested while loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).
Infinite While Loop
If we pass TRUE in while loop, it will be an infinite loop.
while(true)
{
//code to be executed
}
Example - infinite while loop
The PHP do...while Loop
The do...while loop will always execute the block of code once In PHP do..while loop is similar to while loop but one key difference between them.The do...while loop will always execute the block of code once. If the the condition is true then code of block executed repeatedly, until the condition becomes false.
But in while loops execute a block of code while the specified condition is true.
do {
code to be executed;
} while (condition is true);
The PHP for Loop
The for loop is used when you know in advance how many times the script should run.
for (init counter; test counter; increment counter) {
code to be executed for each iteration;}
Parameters:
The parameters of for loop have following meanings:
- initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional.
- condition - Evaluate each iteration value. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.
- Increment/decrement - It increments or decrements the value of the variable.
All three parameters are optional, but semicolon (;) is must to pass in for loop. If we don't pass parameters, it will execute infinite.
Example
Nested For Loop
We can use for loop inside for loop in PHP, it is known as nested for loop. The inner for loop executes only when the outer for loop condition is found true.
In case of inner or nested for loop, nested for loop is executed fully for one outer for loop. If outer for loop is to be executed for 3 times and inner for loop for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).
The PHP foreach Loop
This loop works only on arrays, and is used to loop through each key or value pair in an array. It will issue an error if you try to use it with the variables of different datatype.
The foreach loop works on elements basis rather than index. It provides an easiest way to iterate the elements of an array. In foreach loop, we don't need to increment the value.
foreach ($array as $value) {
code to be executed;}
There is one more syntax of foreach loop.
foreach ($array as $key => $element)
{
//code to be executed
}
Dynamic array
PHP Break
PHP break statement breaks the execution of the current for, while, do-while, switch, and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.
The break keyword immediately ends the execution of the loop or switch structure. It breaks the current flow of the program at the specified condition and program control resumes at the next statements outside the loop.
The break statement can be used in all types of loops such as while, do-while, for, foreach loop, and also with switch case.
jump statement; break;
PHP Continue
Continue statement breaks one iteration in the loop, if a specified condition occurs, and continues with the next iteration in the loop.
The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.
The continue statement can be used with all types of loops such as - for, while, do-while, and foreach loop. The continue statement allows the user to skip the execution of the code for the specified condition.
jump-statement; continue;
Break and Continue in While Loop
You can also use break and continue in while loops: