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


Popular Tutorials

PHP Tutorial PHP Introduction For Beginners PHP Advantage PHP Software Requirement PHP WAMP Installation PHP XAMPP Installation PHP Syntax PHP Comments PHP Variables PHP $ and $$ Variables PHP Super global variables How to swap two numbers in PHP PHP Constant PHP:Magic Constant echo and print in PHP PHP Data Types PHP Form GET Method POST Method Form action PHP extract() Function PHP Display result in Textbox Multiple submit buttons php different actions Add two textbox values and display the result in third textbox PHP Operators PHP Arithmetic Operators PHP Assignment Operators PHP Comparative Operators PHP Logical Operators PHP Operators Precedence PHP Conditional Statement PHP if Statement PHP if-else Statement PHP if elseif else Statement PHP Switch Statement PHP calculator Profit and Loss What is Loop PHP For Loop Print star pattern in PHP Create table using for loop in PHP PHP While Loop PHP Do While Loop PHP Foreach Loop PHP String PHP String Functions PHP empty() function PHP str_split() Function PHP Numeric Functions PHP Tutorial PDF For Beginners PHP isset function PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP explode() Function PHP fprintf() Function PHP get_html_translation_table() Function PHP hebrev() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function