FOR LOOP in php

Share:

FOR LOOP (PHP)

Loops are repetition structures that are used to perform the same task again and
again till your condition is true.

for loops are used to execute same block of code again and again for the
specified number of times.

If we know the exact iteration of loop then for loop must be used.

General PHP syntax
for(initialization; condition; increment/decrement)
{
// loop section
}
for (initialization; condition; increment/decrement)

In the second part, we set the condition for the
continuation/termination of loop

In each iteration, if the condition evaluates to true then loop continues otherwise

if it evaluates to false then loop terminates

Examples
for ($i = 0; $i<5 i="" p="">{
echo “value of i is $i
” ;
}

$square = 0 ;
for($i = 0 ; $i<=10; $i++)
{
$square = $i*$i ;
echo “Square of $i is $square
” ;
}
?>
Output of program
Square of 0 is 0
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 30
Square of 7 is 35
Square of 8 is 40
Square of 9 is 45
Square of 10 is 50

No comments