PHP – Loop Types,Loops in PHP are used to execute the equal block of code a detailed wide variety of times. PHP supports following four loop types.
- For − loops thru a block of code a certain variety of times.
- While − loops thru a block of code if and as long as a distinctive circumstance is proper.
- Do…Even as − loops thru a block of code as soon as, after which repeats the loop so long as a unique circumstance is real.
- Foreach − loops via a block of code for every detail in an array.
We will talk approximately continue and smash keywords used to control the loops execution.
PHP – Loop Types,The for loop declaration
The for declaration is used when you know how regularly you want to execute a declaration or a block of statements.
Syntax
for (initialization; condition; increment){
code to be executed;
}
The initializer is used to set the begin price for the counter of the wide variety of loop iterations. A variable may be declared here for this cause and it’s far traditional to call it $i.
Example The following instance makes 5 iterations and adjustments the assigned price of variables on each pass 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 the 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
PHP – Loop Types,The while loop statement
The whilst statement will execute a block of code if and as long as a take a look at expression is real.
If the check expression is true then the code block might be done. After the code has done the test expression will again be evaluated and the loop will hold till the check expression is found to be fake.
Syntax
while (condition) {
code to be executed;
}
Example
This example decrements a variable cost on every new release of the loop and the counter increments until it reaches 10 when the assessment is fake and the loop ends.
<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
PHP – Loop Types,The do…while loop statement
The do…Whilst statement will execute a block of code at least as soon as – it then will repeat the loop so long as a circumstance is real.
Syntax
do {
code to be executed;
}
while (condition);
Example
The following instance will increment the value of i at least once, and it’s going to preserve incrementing the variable i as lengthy because it has a price of 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
PHP – Loop Types,The foreach loop statement
The foreach statement is used to loop via arrays. For every bypass the fee of the contemporary array element is assigned to $value and the array pointer is moved by one and within the subsequent pass subsequent element will be processed.
Syntax
foreach (array as value) {
code to be executed;
}
Example
Try out following example to list out 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 −
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
PHP – Loop Types,The break statement
The PHP spoil key-word is used to terminate the execution of a loop upfront.
The ruin statement is situated within the announcement block. It gives you full manage and every time you need to go out from the loop you may come out. After popping out of a loop instant declaration to the loop can be accomplished.
Example
In the subsequent example situation test will become real when the counter cost reaches three and 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 hold keyword is used to halt the modern-day generation of a loop however it does now not terminate the loop.
Just just like the spoil announcement the hold declaration is situated within the statement block containing the code that the loop executes, preceded by a conditional test. For the skip encountering keep assertion, relaxation of the loop code is skipped and subsequent pass starts offevolved.
Example
In the following instance loop prints the cost of array but for which condition turns into real it simply skip the code and next cost is printed.
<html>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
if( $value == 3 )continue;
echo "Value is $value <br />";
}
?>
</body>
</html>
This will produce the following result −
Value is 1
Value is 2
Value is 4
Value is 5