PHP For Loop

PHP Do While Loop Previous Next PHP Foreach Loop


PHP For - loops through a block of code a specified number of times.

Parameters

Initvalue:This is the initial value of the counter

Condition:This is evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.

Increment:This is used to increment the value of the counter variables

Syntax


for(initialize value; condition; increment){ // code to be executed }

Here is the code of php while loop.

Filename: php / for-loop.php


Code
<?php
for($i=1;$i<=6;$i++){
echo $i . "
"; } ?>

Running Step.


Type Url - https://localhost/php/for-loop.php

Output
1
2
3
4
5
6

PHP For Loop Without <br> Tag.

For loop without <br> tag will give output in same line.

Filename: php / for-loop1.php


Code
<?php
for($i=1;$i<=6;$i++){
echo $i;
}
?>

Running Step.


Type Url - https://localhost/php/for-loop1.php

Output
123456

More Code Examples


PHP Decrease For Loop.

Decrease for loop will give output in descending order.

Filename: php / Decrease-for-loop.php


Code
<?php
for($i=5;$i>1;$i--){
echo $i . "
"; } ?>

Running Step.


Type Url - https://localhost/php/Decrease-for-loop.php

Output
6
5
4
3
2
1

PHP Decrease For Loop Without <br> Tag

Decrease for loop without <br> tag will give output in descending order with same line..

Filename: php / Decrease-for-loop1.php


Code
<?php
for($i=5;$i>1;$i--){
echo $i;
}
?>

Running Step.


Type Url - https://localhost/php/Decrease-for-loop1.php

Output
654321

Tags: PHP


Was this helpful ?

Related Code


Popular Code

Latest Code