Understanding PHP Loops: For, While, Do-While Explained with Break and Continue

Hello there! 👋 I'm Saurabh Kailas Damale, a passionate Full Stack Developer with a strong background in creating dynamic and responsive web applications. I thrive on turning complex problems into elegant solutions, and I'm always eager to learn new technologies and tools to enhance my development skills.
Today, we will explore loops in PHP, which allow you to execute a block of code multiple times based on certain conditions. Loops are essential for tasks that require repetitive actions, such as iterating through arrays or processing data in batches. We will cover three types of loops in PHP: for, while, and do-while. Additionally, we will delve into the break and continue statements which help control loop execution.
Using break and continue
break Statement:
The break statement is used to exit a loop prematurely when a certain condition is met. It can be used in for, while, do-while, and switch statements.
Example:
<?php
for ($i = 0; $i < 10; $i++) {
if ($i == 5) {
break; // Exit the loop when $i is 5
}
echo "The number is: $i <br>";
}
// Outputs: The number is: 0, The number is: 1, The number is: 2, The number is: 3, The number is: 4
?>
continue Statement:
The continue statement is used to skip the rest of the current iteration and proceed to the next iteration of the loop. It can be used in for, while, and do-while loops.
Example:
<?php
for ($i = 0; $i < 10; $i++) {
if ($i % 2 == 0) {
continue; // Skip even numbers
}
echo "The number is: $i <br>";
}
// Outputs: The number is: 1, The number is: 3, The number is: 5, The number is: 7, The number is: 9
?>
Using break and continue with Loops
for Loop
The
forloop is used when you know in advance how many times you want to execute a statement or a block of statements. It consists of three parts: initialization, condition, and increment/decrement.Example:
<?php for ($i = 0; $i < 5; $i++) { // Initialize $i to 0; Run loop while $i is less than 5; Increment $i by 1 after each iteration echo "The number is: $i <br>"; // Output the current value of $i } // Outputs: The number is: 0, The number is: 1, The number is: 2, The number is: 3, The number is: 4 ?>while Loop
The
whileloop is used when you want to repeat a block of code as long as a condition is true. The condition is evaluated before the execution of the loop's body.Example:
<?php $i = 0; // Initialize $i to 0 while ($i < 5) { // Run loop while $i is less than 5 echo "The number is: $i <br>"; // Output the current value of $i $i++; // Increment $i by 1 after each iteration } // Outputs: The number is: 0, The number is: 1, The number is: 2, The number is: 3, The number is: 4 ?>do-while Loop
The
do-whileloop is similar to thewhileloop, but it ensures that the block of code is executed at least once. The condition is evaluated after the execution of the loop's body.Example:
<?php $i = 0; // Initialize $i to 0 do { echo "The number is: $i <br>"; // Output the current value of $i $i++; // Increment $i by 1 after each iteration } while ($i < 5); // Run loop while $i is less than 5 // Outputs: The number is: 0, The number is: 1, The number is: 2, The number is: 3, The number is: 4 ?>
Tricky Questions
What will be the output of the following code and why?
<?php for ($i = 0; $i < 5; $i++) { for ($j = 0; $j < 3; $j++) { if ($i == 2) { break 2; } echo "i = $i, j = $j<br>"; } } ?>What is the difference between
whileanddo-whileloops, and when would you use one over the other?How would you optimize the following code to avoid calling the
count()function in each iteration of the loop?<?php $array = [1, 2, 3, 4, 5]; for ($i = 0; $i < count($array); $i++) { echo $array[$i]; } ?>What will be the output of the following code and why?
<?php $i = 10; while ($i--) { if ($i == 5) { continue; } echo $i . " "; if ($i == 2) { break; } } ?>
Conclusion
Today, we explored the three main types of loops in PHP: for, while, and do-while. We also looked into the break and continue statements, which provide additional control over loop execution. Understanding how to use loops effectively is crucial for writing efficient and maintainable code.



