PHP Loops are used to execute the same block of code more than one times.
Types of Loops
for (initialization; condition; increment)
{
code to be executed;
}
Example
The following example makes five iterations and changes the assigned value of two 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
If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.
Syntax
while (condition)
This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false 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
<html>
<body>
<?php
$i = 0;
$num = 0;
do {
$i++;
}
while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>
</body>
</html>
Types of Loops
- for - Executes the block of code more than one time given in for loop
- while - loops through a block of code if and as long as a specified condition is true.
- do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true.
- foreach - loops through a block of code for each element in an array.
- break
- continue
The for loop :
The for statement is used when you know how many times you want to execute a code given in the body of loop or a block of statements.
Syntax
for (initialization; condition; increment)
{
code to be executed;
}
Example
The following example makes five iterations and changes the assigned value of two 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
The while loop statement
The while statement will execute a block of code if and as long as a test expression is true.If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.
Syntax
while (condition)
{
code to be executed;
}
Example
code to be executed;
}
Example
This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false 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
The do...while loop statement
The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.Syntax
do { code to be executed; } while (condition);Example
The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −<html>
<body>
<?php
$i = 0;
$num = 0;
do {
$i++;
}
while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>
</body>
</html>
The foreach loop
The foreach statement is used to loop through arrays. For each pass
the value of the current array element is assigned to $value and the
array pointer is moved by one and in the next pass next element will be
processed.
Syntax
foreach (array as value) { code to be executed; }
Example
<html>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>
output :
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
<html>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>
output :
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

Comments
Post a Comment