Link Search Menu Expand Document

For and While Loops

We automate repetition in our code using loops. In this section we’ll explore PHP’s for and while loops.

Table of Contents

  1. For Loops
  2. While

For Loops

A for loop is a counting loops that executes a block of code until a test becomes false. In PHP a for loop works pretty much as you would expect it to.

<?php

    for($i = 100; $i > 0; $i--) {
        echo $i;
    }
?>

Resources

While

A while loop repeatedly executes a block of code while the supplied boolean expression is true.

<?php

    $i = 1;
    while ($i <= 10) {
        echo $i++;
    }

    echo 'Oh my, a humdrum example if I ever saw one.';

?>

Resources