Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

For

for() is a loop structure. It is configured with an initialization expression, an ending expression and an increment expression.

The for() loop executes the initialization expression first, and only once. Then it executes the block, the increment and the ending. When the ending is false, it exits the loop.

The initialization, ending and increment expression may all host several instructions: they have to be separated by comma, rather than semi colons.

Part of the block execution may be skipped by using the continue keyword.

<?php

    /*
    for(initialization; ending; increment) {
        block
    }
    */
    
    for($i = 0; $i < 10; ++$i) {
        print "$i\n";
    }
    
    for($i = 0, $j = 0; $i < 10; ++$i, $j += 2) {
        print "$i $j\n";  // 1 2, 2 4, 3 6....
    }

?>

Documentation

See Also