Loops in PHP are used to execute the same block of code a specific number of times. PHP supports tracing four types of loops.
for − loops through a block of code a
- specified number of times.
-
while − loops through a block of code as long as a specified condition is true.
-
-
do… while − loops through a block of code once, and then repeats the loop whenever a special condition is met.
-
foreach − loops through a block of code for each element of an array.
We will discuss about the continue and break keywords used to control the execution of loops
. The for loop statement
The
for statement is
used when you know how many times you want to execute a statement or block of instructions.
Syntax
for (initialization; condition; increase) { code to execute; }
The initializer is used to set the initial value for the number of loop iterations counter. Here a variable can be declared for this purpose and it is traditional to name it $i.
Example
The following example performs five iterations and changes the assigned value of two variables at each step of the loop −
<html> <body> <?php $a = 0; $b = 0; for( $i = 0; $i<5; $i++ ) { $a += 10; $b += 5; } echo (“At the end of loop a = $a and b = $b” ); ?> </body> </html>
This will produce the following result: At the
end of the loop a = 50 and b = 25 The while loop statement
The while
statement
will execute a block of code as long as a test expression is true.
If the test expression is true, the code block will be executed. After the code is executed, the test expression will be evaluated again and the loop will continue until the test expression is found to be false.
Syntax
while (condition) { code to be executed; }
Example
This example decreases one variable value at each iteration of the loop and the counter is incremented until it reaches 10 when the evaluation is false and the loop terminates.
<html> <body> <?php $i = 0; $num = 50; while( $i < 10) { $num-; $i++; } echo (“Loop stopped at i = $i and num = $num” ); ?> </body> </html>
This will produce the following result:
loop stopped at i = 10 and num = 40
The do… while the loop statement
The do… While the instruction will execute a block of code at least once, it will repeat the loop whenever a condition is true.
Sintax
do { código a execute; } while (condition);
Example
The following example will increment the value
of i at least once, and continue to increment the variable i whenever it has a value less than 10 −
<html> <body> <?php $i = 0; $num = 0; do { $i++; } while( $i < 10 ); echo (“Loop stopped at i = $i” ); ?> </body> </html>
This will produce the following result:
Loop stopped at i = 10 The
foreach loop statement The foreach statement
is used to loop through arrays. For each pass, the value of the current array element is mapped to $value and the array pointer is moved by one and the next pass will process the next element.
Foreach syntax
(array as value) { code to execute; }
Example
Try the following example to enumerate the values of an array.
<html> <body> <?php $array = array( 1, 2, 3, 4, 5); foreach( $array as $value ) { echo “Value is $value <br />”; } ?> </body> </html>
This will produce the following result:
The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The break
statement
The
PHP break keyword is used to terminate the execution of a loop prematurely
.
The break statement is located inside the instruction block. It gives you full control and whenever you want to get out of the loop you can exit. After exiting a loop, an immediate instruction to the loop will be executed.
<img src=”https://www.tutorialspoint.com/php/images/cpp_break_statement.jpg” alt=”PHP Break Statement” />
Example
In the following
example, the condition test becomes true when the counter value reaches 3 and the loop terminates.
<html> <body> <?php $i = 0; while( $i < 10) { $i++; if( $i == 3 )break; } echo (“Loop stopped at i = $i” ); ?> </body> </html>
This will produce the following result:
Loop stopped at i = 3 The continue
statement The
PHP continue keyword is used to stop the current iteration of a loop, but does not end the loop.
Like the break statement, the
continue statement is located inside the instruction block that contains the code that executes the loop, preceded by a conditional test. For the pass finding continue statement, the rest of the loop code is ignored and the next pass begins.
<img src=”https://www.tutorialspoint.com/php/images/cpp_continue_statement.jpg” alt=”PHP Continue Statement” />
Example
In the following example, the loop prints the array value,
but for which condition it becomes true, it simply omits the code and prints the next value.
<html> <body> <?php $array = array( 1, 2, 3, 4, 5); foreach( $array as $value ) { if( $value == 3 )continue; echo “The value is $value <br />”; } ?> </body> </html>
This will produce the following result: the
value is 1 The value is 2 The value is 4 The value is 5