Loops are used to execute the same block of code a specified number of times or while a specified condition is true. Loops dramatically reduce the amount of code needed to perform repetitive tasks.
PHP supports four main looping structures:
while– Loops through a block of code as long as a specified condition is true.do...while– Loops through a block of code once, and then repeats the loop as long as a specified condition is true.for– Loops through a block of code a specified number of times.foreach– Loops through blocks of code for each element in an array (covered in a later chapter, but included here).
1. The while Loop
The while loop checks the condition before executing the code block. If the condition is initially false, the code block will never execute.
| Syntax | Description |
while (condition) | As long as this condition remains TRUE, the code inside the braces will run. |
Example:
This loop prints the numbers from 1 to 5.
PHP
<?php
$i = 1; // Initialization
while ($i <= 5) { // Condition
echo "The number is: " . $i . "<br>";
$i++; // Increment (Crucial to prevent infinite loops!)
}
?>
2. The do...while Loop
The do...while loop executes the code block at least once, and then checks the condition. If the condition is true, the loop will repeat.
| Syntax | Description |
do { code } while (condition); | Execute the code block once, then check the condition. Repeat if TRUE. |
Example:
Even though $i starts at 6, this code will execute once because the condition is checked after the first run.
PHP
<?php
$i = 6;
do {
echo "The number is: " . $i . "<br>";
$i++;
} while ($i <= 5); // This condition is false, but it runs once.
?>
3. The for Loop
The for loop is used when you know exactly how many times you want to loop through a block of code. It condenses the setup, condition, and increment into a single line.
| Syntax | Description |
for (init counter; test counter; increment counter) | 1. Initialize; 2. Test Condition; 3. Increment/Decrement. |
Example:
This loop prints the numbers from 1 to 10 in a more compact syntax.
PHP
<?php
// (1) Initialization (2) Condition (3) Increment
for ($x = 0; $x <= 10; $x++) {
echo "The count is: $x <br>";
}
?>
4. The foreach Loop (Introduction)
The foreach loop is specifically designed to iterate over Arrays. It runs once for every element in the array. Since we haven’t covered arrays yet, we will introduce the concept here and fully explore it later.
| Syntax | Description |
foreach ($array as $value) | For every item in $array, assign its value to the temporary variable $value and run the code block. |
Example:
PHP
<?php
$colors = array("red", "green", "blue");
foreach ($colors as $value) {
echo $value . "<br>";
}
// Output: red, green, blue
?>
5. Breaking and Continuing Loops
Two special keywords allow you to control the flow within any loop:
| Keyword | Action |
break | Immediately exits the loop entirely. The program continues running any code after the loop. |
continue | Immediately skips the rest of the current iteration and jumps to the next iteration of the loop. |
Example using break:
PHP
<?php
for ($i = 0; $i < 10; $i++) {
if ($i == 4) {
break; // Stop the loop when i reaches 4
}
echo $i . "<br>";
}
// Output: 0, 1, 2, 3
?>
Next Steps
Now that you can control program flow using conditions and repetition using loops, the next fundamental concept is how to manage groups of related data efficiently. The next chapter will cover “PHP Arrays.”
