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

Related Topics

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.

PHP lcfirst() Function

PHP lcfirst() Function The lcfirst() function in PHP converts the first character of a string to lowercase if that character is alphabetic. Syntax lcfirst ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with the...

1 minute read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.

PHP Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are...

3 minutes read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP Do While Loop

PHP do while loop is used to execute the code at least one time because condition is checked after executing the code. Syntax: do{ //code to be executed  }while(condition); Output <?php $n=1;  do{ echo "$n<br/>";  $n++;  }while($n<=5);  ?> Output 1 2 3 4 5 ← Prev Next →...

1 minute read.

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP is_integer() Function

The PHP is_integer() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP chunk_split() Function

PHP chunk_split() Function The chunk_split() function in PHP splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter string(required)- It specifies the string to split. length(optional)- This parameter represents a number that defines the...

1 minute read.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.