for and while Loops
Repeat code with for, while, and do-while loops.
for and while Loops is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Loops?
Loops let you repeat a block of code without copy-pasting it. PHP provides four looping constructs:
for— known iteration countwhile— condition-controlleddo-while— executes at least onceforeach— 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 4for Loop Counting Down
You can count down or step by any amount:
<?php
// Count down
for ($i = 10; $i >= 1; $i--) {
echo $i . ' ';
}
// Step by 2
for ($i = 0; $i <= 10; $i += 2) {
echo $i . ' '; // 0 2 4 6 8 10
}while Loop
A while loop runs as long as its condition is true — checked before each iteration:
<?php
$n = 1;
while ($n <= 5) {
echo $n . ' ';
$n++;
}
// Output: 1 2 3 4 5do-while Loop
A do-while always executes the body at least once — condition checked after:
<?php
$attempt = 0;
do {
echo 'Trying... attempt ' . ($attempt + 1) . PHP_EOL;
$attempt++;
} while ($attempt < 3 && !connectToServer());
function connectToServer(): bool { return false; }Infinite Loop and break
An infinite loop runs forever unless you break out of it:
<?php
$count = 0;
while (true) {
$count++;
if ($count >= 5) {
break; // exit loop
}
}
echo $count; // 5continue — Skip an Iteration
continue skips the rest of the current iteration and jumps to the next:
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 === 0) {
continue; // skip even numbers
}
echo $i . ' '; // 1 3 5 7 9
}Nested Loops
Loops can be nested — handy for 2D data like grids or tables:
<?php
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 3; $col++) {
echo "($row,$col) ";
}
echo PHP_EOL;
}
// (1,1) (1,2) (1,3)
// (2,1) (2,2) (2,3)
// (3,1) (3,2) (3,3)Breaking Nested Loops
Use break 2 (or any number) to break out of multiple loop levels at once:
<?php
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
if ($i === 2 && $j === 2) {
break 2; // exits both loops
}
echo "$i,$j ";
}
}Loop Performance Tips
Avoid expensive operations inside loop conditions:
<?php
// Bad: count() called on every iteration
for ($i = 0; $i < count($arr); $i++) { /* ... */ }
// Good: calculate once
$len = count($arr);
for ($i = 0; $i < $len; $i++) { /* ... */ }
// Also fine: foreach avoids this entirely
foreach ($arr as $item) { /* ... */ }Alternative Loop Syntax
Like if, loops support alternative colon syntax for embedding in HTML:
<?php $items = ['apple', 'banana', 'cherry']; ?>
<ul>
<?php for ($i = 0; $i < count($items); $i++): ?>
<li><?= htmlspecialchars($items[$i]) ?></li>
<?php endfor; ?>
</ul>Quick Check
Which loop is guaranteed to execute its body at least once?
Recap: for and while Loops
Loop summary:
for— best when iteration count is knownwhile— condition checked before each iterationdo-while— body runs at least oncebreakexits a loop;break Nexits N levelscontinueskips to the next iteration
Next: foreach for iterating arrays.
Frequently asked questions
Is the “for and while Loops” lesson free?
Yes — the full text of “for and while Loops” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “for and while Loops”?
Repeat code with for, while, and do-while loops. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “for and while Loops” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if, elseif, and else
- Switch and Match Statements
- for and while Loops
- foreach and Loop Control