Loop in PHP - For Loop
So we are going to understand the for loop.
The Value ot the Intiator : 1
The Value ot the Intiator : 2
The Value ot the Intiator : 3
The Value ot the Intiator : 4
The Value ot the Intiator : 5
The Value ot the Intiator : 4
The Value ot the Intiator : 3
The Value ot the Intiator : 2
The Value ot the Another Variabe : 1
The Value ot the Another Variabe : 2
The Value ot the Another Variabe : 3
The Value ot the Another Variabe : 4
4
6
8
10
12
14
16
18
20
As you know the loop is a re-execute of a block code for the some condition to be true.
For example, I will keep waking till I reached the temple. Here Temple is a condition and walk is the block of code to be executed.
The structure of the for loop as follow:
for ( {initial value for the loop} ; { contion to be check } ; { initial value to be changed } )
{
}
####################################
PHP Code
####################################
<html>
<head>
<title>Loop Example- for</title>
</head>
<body>
<h3>
Example 1
</h3>
<?php
for ( $i = 1 ; $i < 5 ; $i++ )
{
echo " <br/> The Value ot the Intiator : " . $i ;
}
?>
<hr/>
<h3>
Example 2
</h3>
<?php
for ( $j = 5 ; $j > 1 ; $j-- )
{
echo " <br/> The Value ot the Intiator : " . $j ;
}
?>
<hr/>
<h3>
Example 3
</h3>
<?php
$i = 1;
for ( $j = 5 ; $j > 1 ; $j-- )
{
echo " <br/> The Value ot the Another Variabe : " . $i ;
$i++;
}
?>
<hr/>
<h3>
Example 3 : The Table - One Value
</h3>
<?php
$i = 2; // table to be created for
for ( $j = 1 ; $j <= 10 ; $j++ )
{
echo $i * $j;
echo " <br/> ";
}
?>
<hr/>
<h3>
Example 3 : The Table - Range of Value or called loop within loop
</h3>
<?php
echo " <table style='border: 2px solid #666666;'> ";
echo " <tr style='border: 2px solid #666666;'> ";
for ( $i = 1 ; $i <= 20 ; $i++ )
{
echo " <td style='border: 2px solid #666666; text-align:center;'> ";
for ( $j = 1 ; $j <= 10 ; $j++ )
{
echo $i * $j;
echo " <br/> ";
}
echo " </td> ";
}
echo " </tr> ";
echo " </table> ";
?>
<hr/>
</body>
</html>
####################################
Oouput of the Above code
####################################
Example 1
The Value ot the Intiator : 1
The Value ot the Intiator : 2
The Value ot the Intiator : 3
The Value ot the Intiator : 4
Example 2
The Value ot the Intiator : 5
The Value ot the Intiator : 4
The Value ot the Intiator : 3
The Value ot the Intiator : 2
Example 3
The Value ot the Another Variabe : 1
The Value ot the Another Variabe : 2
The Value ot the Another Variabe : 3
The Value ot the Another Variabe : 4
Example 3 : The Table - One Value
24
6
8
10
12
14
16
18
20
Example 3 : The Table - Range of Value or called loop within loop
1 2 3 4 5 6 7 8 9 10 | 2 4 6 8 10 12 14 16 18 20 | 3 6 9 12 15 18 21 24 27 30 | 4 8 12 16 20 24 28 32 36 40 | 5 10 15 20 25 30 35 40 45 50 | 6 12 18 24 30 36 42 48 54 60 | 7 14 21 28 35 42 49 56 63 70 | 8 16 24 32 40 48 56 64 72 80 | 9 18 27 36 45 54 63 72 81 90 | 10 20 30 40 50 60 70 80 90 100 | 11 22 33 44 55 66 77 88 99 110 | 12 24 36 48 60 72 84 96 108 120 | 13 26 39 52 65 78 91 104 117 130 | 14 28 42 56 70 84 98 112 126 140 | 15 30 45 60 75 90 105 120 135 150 | 16 32 48 64 80 96 112 128 144 160 | 17 34 51 68 85 102 119 136 153 170 | 18 36 54 72 90 108 126 144 162 180 | 19 38 57 76 95 114 133 152 171 190 | 20 40 60 80 100 120 140 160 180 200 |
Comments
Post a Comment