PHP - Statement For loop

Introduction

The for loop defines an initialization expression, an exit condition, and the end of an iteration expression.

When PHP first encounters the loop, it executes what is defined as the initialization expression.

Then, it evaluates the exit condition and if it resolves to true, it enters the loop.

After executing everything inside the loop, it executes the end of the iteration expression.

Demo

<?php
     for ($i = 1; $i < 10; $i++) { 
        echo $i . " "; 
     } /*  w w w .  j  a  v a  2 s  . c om*/
?>

Result

The initialization expression is $i = 1, and is executed only the first time.

The exit condition is $i < 10, and it is evaluated at the beginning of each iteration.

The end of the iteration expression is $i++, which is executed at the end of each iteration.

This example prints the numbers from 1 to 9.

Related Topics

Exercise