Loop Liberator: Harnessing Breaks for Precision and Progress
We looked at the break statement when we covered switch statements. In that case, the break statement was used to end the execution of the switch statement. The break statement isn’t exclusive to the switch statement: it can be used in loops as well.
Let’s look at how the break statement ends execution in each of the loops: while, do-while, for, and foreach. We’ll start off by creating an array that the loops will iterate through.
<?php
$favorite_programming_language = [
"java",
"javascript",
"php",
"c",
"c#",
"c++"
];
?>
The goal of each loop will be to iterate through the $favorite_programming_language array and match “php”. Once the element containing “php” is reached, we want to end the execution of the code; there’s no point in continuing the iteration. This will help speed up the execution time since PHP doesn’t have to iterate through each element in the array (unless the value is stored as the last element).
<?php
$i = 0;
echo "While Loop<br>";
while ( $i < count($favorite_programming_language) ) {
echo $i . "<br>";
if ( $favorite_programming_language[$i] == "php" ) {
echo "PHP is our favorite<br>";
break;
}
$i++;
}
?>
Starting with the while loop. PHP checks to see if the condition inside of the statement evaluates to true. If it does, it executes the code inside of the loop body. It displays the index value $i. If the element’s value matches “php,” the string “PHP is our favorite” is displayed and the code terminates. Let’s walk through the while loop logically. We’ll only do this for the while loop since the logic is almost identical for each other loop.
- PHP initializes $i = 0.
- It echoes out “While Loop.”
- PHP encounters the while keyword.
- It obtains the number of elements inside of the $favorite_programming_language array with the help of the count() function.
- It tests the condition: $i < 6. During the first iteration, $i = 0, so the expression 0 < 6 evaluates to true.
- PHP enters the loop body and echoes out the current value of $i.
- It encounters the if statement. During the first iteration, the if statement evaluates to false since java does not equal php. PHP skips the if statement body.
- PHP moves past the closing curly brace of the if statement and increments $i. In the first iteration, $i is incremented from 0 to 1.
- PHP repeats steps 4 through 9 for the second iteration.
- During the third iteration, PHP sees that the if statement condition evaluates to true.
- PHP enters the body of the if statement and echoes out “PHP is our favorite.”
- PHP encounters the break statement and terminates the loop. It continues code execution after the while loop’s closing curly brace.
Once the break statement is encountered inside of a loop, it will skip over all of the other code that’s inside of the loop and will continue code execution outside of the loop body. Even though the break statement is located inside of the loop body, it will still terminate the loop. If a break statement is encountered inside of a nested loop (loop within a loop), it will terminate the loop where it’s located. The outer loop will still continue to execute.
Let’s move on to the do-while loop.
<?php
$i = 0;
echo "Do While Loop<br>";
do {
if ( $favorite_programming_language[$i] == "php" ) {
echo "PHP is our favorite<br>";
break;
}
$i++;
} while ( $i < count($favorite_programming_language) );
?>
The difference between the while and the do-while loop is that PHP will check the condition of the if statement before checking the condition of the do-while loop (opposite of the while loop). Once “php” is matched, the break statement will be executed and the loop will terminate.
We’ll look at the for loop next. Again, once the break statement is encountered, the for loop will terminate.
<?php
echo "For Loop<br>";
for ( $i = 0; $i < count($favorite_programming_language); $i++ ) {
if ( $favorite_programming_language[$i] == "php" ) {
echo "PHP is our favorite<br>";
break;
}
}
?>
And finally, the foreach loop.
<?php echo "Foreach Loop<br>"; foreach( $favorite_programming_language as $language ) { if ( $language == "php" ) { echo "PHP is our favorite<br>"; break; } } ?>
Since this is an article on the break statement, let’s briefly cover how it’s used inside of the switch statement. If you need a refresher on how the switch statement works, you can read about it here.
<?php
$num_of_subscribers = 1000521;
switch( $num_of_subscribers ) {
case ( $num_of_subscribers > 1000000 ):
echo "I'm living the life of the rich and the famous";
break;
case ( $num_of_subscribers >= 100000 ):
echo "I quit my job today";
break;
case ( $num_of_subscribers >= 1000 ):
echo "I'm starting to make pennies now";
break;
default:
echo "Keep your day job";
}
?>
In the example above:
- PHP assigns the value 1000521 to $num_of_subscribers.
- PHP encounters the switch statement.
- It passes the $num_of_subscribers variable to the switch statement and starts the testing process.
- It looks at the first case and evaluates the expression $num_of_subscribers > 1000000. Since 1000521 is larger than 1000000, it enters the case statement.
- PHP echoes out “I’m living the life of the rich and the famous.”
- PHP moves to the next statement, which just happens to be the break statement. PHP exits the switch statement, meaning that the rest of the cases are not even looked at. PHP continues the execution of the code after the closing curly brace of the switch statement.
One last thing to note is that inside of our loops, sometimes we just want to skip the code inside of the loop body and move on to the next element without terminating the loop. This can be achieved with the continue statement, which is the topic of the next article.
PHP Tutorial Series
Continue your learning with these articles
unleash the power of each: embrace the foreach!
PHP – P32: foreach loop
You can use the for loop to iterate through an array, but to really shine, you need to use the foreach loop. PHP allows you to foreach through arrays.
Loop Liberator: Harnessing Breaks for Precision and Progress
PHP – P33: Break statement
We looked at the break statement when we covered switch statements. In that case, the break statement was used to end the execution of the switch statement. The break statement isn’t exclusive to the switch statement: it can be used in loops as well.
CONDUCTING LOOPS IN HARMONY WITH CONTINUE
PHP – P34: CONTINUE STATEMENT
Need to skip something in a loop? Use the continue statement to do so. Breaks exit loops and continues skip over a single iteration.