PHP for Loop


In this lesson, you will learn how to use loops in PHP code – especially the “For loop.” In php, loops are used to execute the same code for a specified number of times or until an expression is met. This would help developers to write a minimal code while avoiding repetition.

Overview of for loop in PHP

PHP supports four types of loops:

  1. For loop
  2. While loop
  3. Do-while loop
  4. Foreach

The “for loop” is used when the developer has a clear idea about the number he wants to execute a block of code. The number of required iteration has to be known before writing the loop. This is also known as entry-controlled loops. They behave like their C counterparts. It has three main parts or parameters to code: the initialization part, the condition, and the increment or decrement part.

The Syntax of ‘for loop.’

for( expression ; expression or condition ; expression){

Statement;
}

The first expression is evaluated only once at the beginning of the loop.

The second expression or condition is checked at every iteration. If it’s evaluated to be true, the loop continues, and the block of statements nested within the brackets is executed. The execution of the loop will end once the expression is evaluated to be false.

At the end of each iteration, the third expression is evaluated.

Each of this expression can be empty or contain more than on expressions separated by commas. In the second expression, all expressions separated by a comma are evaluated, but the result is taken from the last part. If you leave the second expression empty, the loop will have no breaking point and will run indefinitely. This is known as an infinite loop.

The following diagram shows the sequence of how for loop is executed.

for loop Flowchart

Example I ‘for loop’ in PHP

<?php 
for ($num = 1; $num <= 10; $num ++) { 
echo "$num \n"; 
} 
?>

In this loop, the variable $num is being initialized to 1. The loop will check the condition for the first iteration. As the condition is evaluated as true, the loop will continue and print $num\n and move to increment expression. After $num being incremented, it will move to the condition expression again and so on. The loop will continue to execute until the condition is evaluated as false.

Example II ‘for loop’ in PHP

As mentioned before, you can leave any expression empty in the ‘for loop’. The statement is increasing the number from 1 to 10 and breaks the loop when $i is greater than 10.

for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}

Example III ‘for loop’ in PHP

You can leave all three expressions empty as following.

$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}

Example IV ‘for loop’ in PHP

The following example shows multiple statements in an expression separated by commas.

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

Example V ‘for loop’ in PHP

You can also you the colon syntax of the for loop, in case you want to mix HTML and PHP code.

<ul>
<?php for($i = 1; $i <= 10; $i++): ?>
<li>List Item #. <?php echo $i; ?></li>
<?php endfor; ?>
</ul>
PHP Switch Statement Tutorial Home PHP while loop
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →