saftutorials.com
PHP for loop Tutorial

When you know in advance that how many times the PHP part of code should run, at that time we use for loop.


Syntax of for loop

for (initialization; condition; increment) {

    the code will be executed till condition will be true;

}


Example of while loop

Initially, $a variable is initialized by 1.The for loop starts to execute the code as long as $a is less than 10 and $a will increase by 1 every time loops run.


<!DOCTYPE html>
<html>
<body>
<?php
for($a = 1; $a < 10; $a++) {
?>

<p><?php echo $a." Time"; ?>  : <?php echo $a . ' is less than 10';?></p>
<?php
}
?>

</body>
</html>


The above code produces the following results

Outputs:

1 Time: 1 is less than 10

2 Time: 2 is less than 10

3 Time: 3 is less than 10

4 Time: 4 is less than 10

5 Time: 5 is less than 10

6 Time: 6 is less than 10

7 Time: 7 is less than 10

8 Time: 8 is less than 10

9 Time: 9 is less than 10


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic