Skip to content
AgileLogics
4 min read

Loops

Prerequisites: Match expressions

PHP has three loop constructs: for, while, and foreach (the one you'll use most for arrays).

for ($i = 0; $i < 5; $i++) {
    echo $i;
}

$i = 0;
while ($i < 5) {
    echo $i;
    $i++;
}

foreach (['a', 'b', 'c'] as $letter) {
    echo $letter;
}

Practice

Print the numbers 1 through 5 using a for loop.

Show solution
for ($i = 1; $i <= 5; $i++) {
    echo $i;
}