Home >>PHP Tutorial >PHP For Loop

PHP For Loop

For loop in PHP

The for loop is used when you know in advance how many times the script should run. Syntax
for (initialization; condition; increment)
	
{
	
 code to be executed;
	
} 
		
Parameters of for loop :
Initialization :is used to set a counter.
Condition : if condition is true loop will continue, if the condition is false loop ends.
Increment : used to increment the counter.
Eg i (print the statement 5 times)
<?php
	
for ($i=1; $i<=5; $i++)
	
{  	
echo "The Number is: ".$i."<br/>";
	
} 

?>
Output The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
In the above example
We want to print the statement 5 times. we initialize how many times loop should iterate.
loop starts from ($i=1)and ends($i<=5)
so output displays five times defined statement.

Write a program to print your name 10 times

 <?php
	
$name="rexx";
	
for ($i=1; $i<=5; $i++)
	
{
	
echo "My Name is: ".$name."<br/>";
	
} 

?>
Output
My Name is rexx
My Name is rexx
My Name is rexx
My Name is rexx
My Name is rexx
In the above example
here output displays five times defined statement(My Name is ) with a variable(rexx)

Find the sum of 1 to 100.

 <?php
	
$sum=0;
	
for ($i=1; $i<=100; $i++)
	
{
  	 
 $sum=$sum+$i;   
	
} 
	
echo $sum;

?>

Output
5050
In the above example,
Variable( $sum ) holds value(0). For( ) loop is used to print sum of numbers.
set loop iteration, loop will continue to run as long as ( $i<=100) .
So output display 5050

Find all even numbers between 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,
Here loop starts from ($i=2 ) after every count $i increment its value by 2 and Print all even value from( 1 to 100)

Find all odd numbers between 1 to 100 using loop.

 <?php
	
for ($i=1; $i<=99; $i+=2)
	
{
  	  
echo $i." ";
	   
} 
 
?>
Output
1 3 5 7 9 ... 99
In the above example
Loop start from($i=1) to ($i<=99) every time $i increment its value by 2. if the value of ($i=1) then it will become 3. therefore all odd value are print.

Find the Sum of even and odd numbers between 1 to 100.

<?php
	
for ($i=1; $i<=100; $i++)
	
{
	 
if($i%2==0)
	  
{
	    
@$even=$even+$i;
	  
}
	  
else
	  
{
	  	
@$odd=$odd+$i;
	  
}
	   
	
} 
	 
echo "Sum of even numbers=".$even."<br/>";
	
 echo "Sum of odd numbers=".$odd;
 
?>

Output
Sum of even numbers=2550
Sum of odd numbers=2500
In the above example
For loop is used because we know how many times loop iterate. inside the for loop we declare if..else condition.
If( $%2==0) condition is true, then code will execute and calculate the sum of even number. otherwise else statement execute and calculate the sum of odd number.
Print the sum of odd ans even number separately.

Add two numbers using loop(Not use + operator).

 <?php
	
@$f=$_GET['f'];
	
@$s=$_GET['s'];
	
for ($i=1; $i<=$s; $i++)
	
{
	 
 $f++; 
	
} 
	 
echo "Sum of given numbers=".$f;
 
?>

<body>
 	
<form>
	 
 Enter first number <input type="text" name="f"><br/>

Enter Second number<input type="text" name="s"><br/>
	 
 <input type="submit" value="add">
	
</form>

 </body>
Output
Sum of given numbers=1000
Enter first number
Enter Second number

In the above example
First we create a HTML script to take input from users.
when a value entered by user and click on button ,value redirected to PHP script page.$_GET[ ] is used to collect the value that is entered by user.
Now we check the sum. but sum is generate using (+) operator.
iteration start from ($i=1 to $i<=$s) it means loop depend on value of second textbox.
Value entered by user in first textbox is 500
Value entered by user in second textbox is 500
output become: 1000. because value of first textbox is incremented.
for loop will continue to run as long as ( $i<=$s ) condition is true.

Subtract two numbers using loop(Not use - operator).

<?php
	
@$f=$_GET['f'];
	
@$s=$_GET['s'];
	
for ($i=1; $i<=$s; $i++)
	
{
	  
$f--; 
	
} 
	 
echo "Subtraction of given numbers=".$f;
 
?>

<html>

<body>
 	
<form>
	  
Enter first number<input type="text" name="f"><br/>

Enter Second number<input type="text" name="s"><br/>
	
<input type="submit" value="Subtract">
	
</form>

<body>
Output
Subtraction of given numbers=500
Enter first number
Enter Second number


No Sidebar ads