PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating loops, and it modifies variables after each iteration. Example:

<?php
for ($count = 1 ; $count <= 10 ; ++$count)
echo "$count times 10 is " . $count * 10 . "<br/>";
?>

Output:
1 times 10 is 10
2 times 10 is 20
3 times 10 is 30
4 times 10 is 40
5 times 10 is 50
6 times 10 is 60
7 times 10 is 70
8 times 10 is 80
9 times 10 is 90
10 times 10 is 100
In the above example, we are outputting the 10 times table from a for loop. Each for statement takes three parameters:
  • An initialization expression.
  • A condition expression.
  • A modification expression.
These three parameters are separated by semicolons, for (exp1;exp2; exp3).
  1. The start of the first iteration of the loop, the initialization expression is executed.
  2. Each time around the loop, the condition expression is tested, and the loop is entered only if the condition is TRUE.
  3. At the end of each iteration,the modification expression is executed.
Example (Even Numbers from 1 to 100):
<?php
for ($i=2; $i<=100; $i+=2)
{
 echo $i." ";
}
?>

Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

  • In the above example, for loop from ($i=2) and after every count, $i increment its value by 2, which print all the even value from 1 to 100.
Example (Pattern1):
<?php
for ($i=1; $i<=5; $i++)         
{          
for($j=1;$j<=$i;$j++)           
{                      
echo $j." ";     
}                      
echo "<br/>";           
} 
?>
Output:
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Example (Pattern2):
<?php
for ($i=1; $i<=10; $i++)       
{          
for($j=1;$j<=$i;$j++)           
{                      
echo $i." ";     
}                      
echo "<br/>";           
} 
?>
Output:
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
Example (Pattern3):
<?php
for ($i=1; $i<=5; $i++)         
{          
for($j=1;$j<=$i;$j++)           
{                      
echo " * ";      
}                      
echo "<br/>";           
} 
?>
Output:
* 
* * 
* * * 
* * * * 
* * * * *
Example (Pattern4):
<php
for ($i=1; $i<=10; $i++)       
{          
for ($k=10; $k>$i; $k--)       
{          
//print one space through html ;
echo " ";         
}          
for($j=1;$j<=$i;$j++)           
{                      
echo "*";        
}                      
echo "<br/>";           
}
?>
Output:
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********
Example (Pattern5):
<?php 
for($i=0;$i<=10;$i++)
{ 
for($j=10-$i;$j>=1;$j--)
{ 
echo "* "; 
} 
echo "<br>"; 
} 
?>
Output:
* * * * * * * * * * 
* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*
Example (Pattern6):
<?php
for($i=0;$i<=10;$i++)
{ 
for($k=10;$k>=$i;$k--)
{ 
echo "  "; 
} 
for($j=1;$j<=$i;$j++)
{ 
echo "*  "; 
} 
echo "<br>"; 
} 
for($i=9;$i>=1;$i--)
{ 
for($k=10;$k>=$i;$k--)
{ 
echo "   "; 
} 
for($j=1;$j<=$i;$j++)
{ 
echo "*  "; 
} 
echo "<br>"; 
} 
?>
Output:
                   *  
                 *  *  
               *  *  *  
             *  *  *  *  
           *  *  *  *  *  
         *  *  *  *  *  *  
       *  *  *  *  *  *  *  
     *  *  *  *  *  *  *  *  
   *  *  *  *  *  *  *  *  *  
 *  *  *  *  *  *  *  *  *  *  
   *  *  *  *  *  *  *  *  *  
     *  *  *  *  *  *  *  *  
       *  *  *  *  *  *  *  
         *  *  *  *  *  *  
           *  *  *  *  *  
             *  *  *  *  
               *  *  *  
                 *  *  
                   *  

Example (Pattern7)
<?php
for($i=1; $i<=10; $i++)
{  
for($j=1; $j<=$i; $j++)
{  
echo ' * ';  
} 
echo '<br>';  
} 
for($i=10; $i>=1; $i--)
{  
for($j=1; $j<=$i; $j++)
{ 
echo ' * ';  
}  
echo '<br>';  
}  
?>
Output:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
Example (Table using for loop):
<html>
<body>
<form method="post">
<table align="center">
<tr>
<td>
Enter Number of Rows
</td>
<td>
<input type="text" name="r"/>
</td>
</tr>
<br>
<tr>
<td>
Enter Number of Cols </td>
<td>
<input type="text" name="c"/>
</td>
</tr>
<br>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Create Table" name="create"/>
</td>
</tr>
</form>
</table>
</body>
</html>
<?php
if(isset($_POST['create']))
{
$rows=$_POST['r'];
$cols=$_POST['c'];
echo "<table border='1' align='center'>";
for($i=0;$i<$rows;$i++)
{
echo "<tr>";
for($j=0;$j<$cols;$j++)
{
echo "<th>"."r".$i."c".$j."</th>";
}
echo "</tr>";
}
echo "</table>";
}
?>
Output: PHP for Loop Example