0PricingLogin
PHP Academy · Lesson

for and while Loops

Repeat code with for, while, and do-while loops.

Why Loops?

Loops let you repeat a block of code without copy-pasting it. PHP provides four looping constructs:

  • for — known iteration count
  • while — condition-controlled
  • do-while — executes at least once
  • foreach — iterates arrays/objects (next lesson)

for Loop

The for loop has three parts: init, condition, increment:

<?php
for ($i = 0; $i < 5; $i++) {
    echo $i . ' ';
}
// Output: 0 1 2 3 4

All lessons in this course

  1. if, elseif, and else
  2. Switch and Match Statements
  3. for and while Loops
  4. foreach and Loop Control
← Back to PHP Academy