Loops in PHP

From the PHP cheat sheet · Control Structures · verified Jul 2026

Loops

Different types of loops in PHP

php
// For loop
for ($i = 0; $i < 10; $i++) {
    echo $i;
}

// Foreach loop
foreach ($array as $value) {
    echo $value;
}

foreach ($array as $key => $value) {
    echo "$key: $value";
}

// While loop
while ($condition) {
    // code
}

// Do-while loop
do {
    // code
} while ($condition);
💡 Use foreach for arrays, for when you need index
⚡ Reference (&) in foreach allows modification
📌 Always unset references after foreach
🔥 Break/continue can specify nesting level

More PHP tasks

Back to the full PHP cheat sheet