PHP Loops
When writing code, it's common to want the same block of code to execute a specific number of times. We may utilize loops instead of adding roughly equivalent lines of code to a script.
Loops are used to repeatedly execute the same code block, provided that a given condition is true.
In PHP, we have the following loop types :
for
- Loops over a section of code a certain number of times.while
- While the supplied condition is true, it loops over a block of code.do...while
- Once the block of code is looped, the loop will be repeated until the given conditions are correct.foreach
- For each entry in an array, it loops over a code block.
PHP for Loop
The for
loop- Loops over a section of code a certain number of times.
When you know how many times you want the script to run, you should use for
loop.
Syntax :-
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Parameters :-
- init counter: Set the loop counter value to a reasonable value.
- test counter: Every loop iteration is evaluated. The loop will continue if it returns TRUE. The loop is terminated if its condition is False.
- increment counter: This function raises the value of the loop counter.
Example 1 :- The example below displays the numbers from 1 to 5 :
";
}
?>
Output :-
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example Explained :-
- $x = 1; - Initialize the loop counter ($x), and set the start value to 1.
- $x <= 5; - Continue the loop as long as $x is less than or equal to 5.
- $x++ - Increase the loop counter value by 1 for each iteration.
Example 2 :- This example counts to 50 by tens :
";
}
?>
Output :-
The number is: 20
The number is: 30
The number is: 40
The number is: 50
Example Explained :-
- $x = 10; - Initialize the loop counter ($x), and set the start value to 10.
- $x <= 50; - Continue the loop as long as $x is less than or equal to 50.
- $x+=10 - Increase the loop counter value by 10 for each iteration.
PHP while Loop
While
the supplied condition is true, it loops over a block of code.
A while
loop performs a coding block as long as the conditions stated are true.
Note:- It supports any user defined conditions.
Syntax :-
while (condition) {
code to be executed if condition is true;
}
Example:- The example below displays the numbers from 1 to 5.
";
$x++;
}
?>
Output :-
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example Explained :-
- $x = 1; - Initialize the loop counter ($x), and set the start value to 1.
- $x <= 5 - Continue the loop as long as $x is less than or equal to 5.
- $x++; - Increase the loop counter value by 1 for each iteration
PHP do while Loop
The do while
loop - Once the block of code is looped, then the loop will be repeated until the given conditions are correct.
The do while
loop will always perform the code block once, it checks the condition
and repeats the loop until the condition specified is true.
Syntax :-
do {
code to be executed;
} while (condition);
Example 1 :-
";
$x++;
} while ($x <= 5);
?>
Output :-
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The above example below first sets a variable $x to 1 ($x = 1). Then, the do while
loop will write some
output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop
will continue to run as long as $x is less than, or equal to 5:
Note :- In a do while
loop, the condition is verified AFTER the loop's statements have been executed.
This indicates that the do while
statement will execute its statements at least once,
even if the condition is false.
Example 2 :- This example sets the $x variable to 6, then it runs the loop, and then the condition is checked :
";
$x++;
} while ($x <= 5);
?>
Output :-
Related Links
PHP foreach Loop
Theforeach
- For each entry in an array, it loops over a code block.
The foreach
loop is used to cycle through each key/value pair in an array and is only applicable to arrays.
Syntax :-
foreach ($array as $value) {
code to be executed;
}
The value of the actual array element is assigned to $value for each
loop iteration, and the array indicator is shifted one way until the last array element is reached.
Example 1 :- The following example will output the values of the given array ($colors) :
";
}
?>
Output :-
green
blue
yellow
PHP break Statement
The break
keyword is used exit from current loop or iteration in a looping statements.
Breaking out of a loop is also possible with the break
statement.
It can be used with for
, while
, switch
, and so on.
Example:- This example jumps out of the loop when x is equal to 2 :
";
}
?>
Output :-
The number is: 1
Related Links
PHP continue Statement
The continue
keyword is used to skip one or more iteration in a looping statement.
If a certain condition happens, the continue
statement breaks one or multiple iterations
(in a loop) and continues with the remaining iteration in the loop.
Example :- This example skips the value of 2 :
";
}
?>
Output :-
The number is: 1
The number is: 3
The number is: 4