Looping Statements in PHP
Loops allow you to execute a block of code multiple times, either a set number of times or until a condition is met.
1. while Loop
Executes as long as the condition is true.
2. do...while Loop
Runs the code at least once, then checks the condition.
3. for Loop
Best when you know exactly how many times to loop.
4. foreach Loop
Special loop for arrays — great for looping over lists or associative arrays.
With associative arrays:
5. Loop Control Statements
| Statement | Description |
|---|---|
break |
Exits the loop entirely |
continue |
Skips the current iteration, moves to next |
Summary Table
| Loop Type | Use Case |
|---|---|
while |
When condition is checked first |
do...while |
When loop should run at least once |
for |
When iteration count is known |
foreach |
When working with arrays |
