php for loop



PHP for loop

PHP loop used to execute same block of code again and again specified number of time.

for loop – In for loop execute block of code up to the condition is true.
In for loop condition is predefined.

for loop syntax in PHP

<?php

for ( initialization; condition; increment/decrement)
{
// block of code to be executed
}

?>

Initialization = used to set start counter.
Condition = if condition is true for loop will continue, if condition is false the for loop terminated.
Increment/Decrements = used to increment or decrements start counter.

The initialization define the starting point of loop, In initialization portion we declare variable with value and then check the condition. If the condition is satisfied with the initial value then the block of code to be executed. After first execution of code the increment or decrements will be done as defined in increment/decrement portion of for loop.

After increment initial value check condition this loop execute again and again while the condition is true. When the condition is not satisfied means condition is not true the for loop terminated.

Example of php for loop

<?php

for($i=1; $i<5; $i++)
{
echo $i;
}

?>

The output is:
1234

In above for loop example we declare variable $i with value 1, here the condition is $i<5, for $i=1 the condition is true then execute code print $i.

Then increment 1 to $i,
now $i=2 the condition is true,
$i=3 condition is true,
$i=4 condition is true.
When $i=5 the condition $i<5 is not true then for loop terminated this time.
so for loop execute code four time and out put is : 1234

for loop example

 <?php

for($i=5; $i>0; $i- -)
{
echo $i;
}

?>

The output is:
54321

Here, we start count $i=5 check condition $i>0 after decrements 1 to start counter.

PHP for loop Example

<?php

$name=”meera”;
for($i=1;$i<=5;$i++)
{
echo “Hello “. $name . “<br/>”;
}

?>

The output is:
Hello meera
Hello meera
Hello meera
Hello meera
Hello meera

 

for loop php example

<?php

$sum=0;
for($i=1;$i<=5;$i++)
{
$sum=$sum+$i;
}

echo “The sum of 1 to 5 = “.$sum;

?>

The output is:
The sum of 1 to 5 = 15

Example

<?php

for($i=1;$i<=10;$i=$i+2)
{
echo $i;
}

?>

The output is:
13579

Example to display odd and even no of given number.

<?php

$even=””;
$odd=””;

for($i=1;$i<=50;$i++)
{
if($i%2==0)
{
$even = $even . ” ” .$i ;
}
else
{
$odd = $odd . ” ” .$i;
}
}

echo “The Even no = ” . $even .”<br/>”;
echo “The Odd no = ” . $odd;

?>

The output is:
The Even no = 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
The Odd no = 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Example

<?php

for($i=1;$i<8;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i;
}
echo “<br/>”;
}

?>

The output is:
1
22
333
4444
55555
666666
7777777

Example

<?php

for($i=1;$i<8;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo “<br/>”;
}

?>

The output is:
1
12
123
1234
12345
123456
1234567

Leave a Reply

Your email address will not be published. Required fields are marked *