Module 2 – PHP Fundamentals
Lesson
While Loop
Loop with condition at the start
while executes the body while the condition is true. Suitable for reading files line by line (fgets), processing PDO fetch results while rows remain, and loops with an unknown number of steps. Always ensure the condition becomes false to avoid an infinite loop.
do-while
Executes the body at least once before checking – useful for menus in CLI scripts.
<?php
$broj = 1;
while ($broj <= 10) {
echo $broj . " ";
$broj++;
}Example variation
Extend the lesson example: add another variable, change the loop boundary or branching, and observe the output. Deliberately introduce an error (e.g. an undefined variable) to see the PHP message – then fix it.
