PHP for Loop (original) (raw)

Last Updated : 25 Aug, 2022

The for loop is the most complex loop in PHP that is used when the user knows how many times the block needs to be executed. The for loop contains the initialization expression, test condition, and update expression (expression for increment or decrement).

Flowchart of for Loop:

Syntax:

for (initialization expression; test condition; update expression) { // Code to be executed }

Loop Parameters:

Example 1: The following code shows a simple example using for loop.

PHP `

num=0;num = 0; num=0;num < 20; $num += 5) { echo $num . "\n"; } ?>

`

Example 2: The following code shows another example of for loop.

PHP `

num=1;num = 1; num=1;num < 50; $num++) { if($num % 5 == 0) echo $num . "\n"; } ?>

`

Output

5 10 15 20 25 30 35 40 45

Reference: https://www.php.net/manual/en/control-structures.for.php