PHP Loop Types:-
Syntax:-
- Loops in PHP are used to execute the same block of code a specified number of times.
- PHP supports following four loop types :-
- for loop :- The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax:-
for (initialization; condition; increment)
{
code to be executed;
}
Example :-
for loop.php :-
<html>
<body>
<?php
$a = 0; //Initializing the variable, i.e, a
$b = 0; //the variable "b" has been assigned with a value "0"
for( $i=0; $i<5; $i++ )
{
echo "i's value:- $i<br/>";
$a += 5;
echo "a's value:- $a<br/>";
$b += 3;
echo "b's value:- $b<br/><br/>";
}
echo ("At the end of the loop, <b> i=$i, </b> <b> a=$a </b> and <b> b=$b </b>" );
?>
</body>
</html>
Screenshot :-
- while loop :- The while statement will execute a block of code, if the test expression is true. 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:-
<html>
<body>
<?php
$i = 0;
$num = 30;
echo "Initial Value of <b> i = $i and num = $num </b><br/><br/>";
echo "<b><i> While Loop Starts </i></b><br/><br/>";
while( $i < 5)
{
echo "num's value:- $num<br/>";
$num--;
$i++;
echo "i's value:- $i<br/><br/>";
}
echo ("Loop stopped at <b> i = $i </b> and <b> num = $num </b>" );
?>
</body>
</html>
Screenshot:-
- do...while loop:- 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 :-
<html>
<body>
<?php
$i = 0;
echo "Initial Value of <b> i = $i </b><br/><br/>";
echo "<b><i> Do...While Loop Starts </i></b><br/><br/>";
do
{
$i++;
echo "i's value:- $i<br/><br/>";
}while( $i < 10 );
echo ("Loop stopped at <b> i = $i </b>" );
?>
</body>
</html>
Screenshot:-
- 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);
echo "Array's Data is <b> $array[0] </b> <br/>";
echo "Array's Data is <b> $array[1] </b> <br/>";
echo "Array's Data is <b> $array[2] </b> <br/>";
echo "Array's Data is <b> $array[3] </b> <br/>";
echo "Array's Data is <b> $array[4] </b> <br/><br/>";
echo "<b><i> For Each Loop Starts </i></b><br/><br/>";
foreach( $array as $value )
{
echo "Value is <b> $value </b> <br/> ";
}
?>
</body>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
echo "Array's Data is <b> $array[0] </b> <br/>";
echo "Array's Data is <b> $array[1] </b> <br/>";
echo "Array's Data is <b> $array[2] </b> <br/>";
echo "Array's Data is <b> $array[3] </b> <br/>";
echo "Array's Data is <b> $array[4] </b> <br/><br/>";
echo "<b><i> For Each Loop Starts </i></b><br/><br/>";
foreach( $array as $value )
{
echo "Value is <b> $value </b> <br/> ";
}
?>
</body>
</html>
Screenshot:-
No comments:
Post a Comment