Recall & Review
beginner
What does the
break statement do in PHP loops?The
break statement immediately stops the execution of the current loop and exits it.Click to reveal answer
intermediate
How does
break with levels work in PHP?You can specify a number after
break to exit multiple nested loops at once. For example, break 2; exits two levels of loops.Click to reveal answer
beginner
What happens if you use
break 1; in a nested loop?Using
break 1; exits only the innermost loop, which is the default behavior of break without a number.Click to reveal answer
intermediate
Can you use
break with levels in switch statements in PHP?Yes,
break with levels works in both loops and switch statements. It exits the specified number of enclosing loop or switch structures.Click to reveal answer
intermediate
Example: What does this code output?<br>
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
break 2;
}
echo "$i$j ";
}
}The output is
11 . When $j equals 2, break 2; exits both loops immediately, so only 11 is printed.Click to reveal answer
What does
break 3; do inside three nested loops?✗ Incorrect
break 3; exits three levels of loops, so it stops all three nested loops immediately.
What is the default level of
break if no number is specified?✗ Incorrect
By default, break exits only the innermost loop, which is level 1.
Can
break 2; be used inside a single loop?✗ Incorrect
If you use break with a level greater than the number of nested loops, PHP will issue a warning.
Which statement is true about
break in PHP?✗ Incorrect
break can exit multiple nested loops by specifying a number after it.
What will happen if you use
break 0; in a loop?✗ Incorrect
Using break 0; is invalid and causes a fatal error in PHP.
Explain how the
break statement with levels works in PHP nested loops.Think about how many loops you want to stop at once.
You got /4 concepts.
Describe a real-life situation where breaking out of multiple loops at once might be useful.
Imagine looking for something in a big table and stopping when found.
You got /4 concepts.