for loops: outer loop for shelves (1 to 3), inner loop for books (1 to 5).targetBook with the value 3 to represent the book to find.break 2; to exit both loops when the target book is found.for loops: outer loop for shelves (1 to 3), inner loop for books (1 to 5).targetBook with the value 3 to represent the book to find.break 2; to exit both loops when the target book is found.for loop with variable $shelf from 1 to 3 and inside it create another for loop with variable $book from 1 to 5.Use two for loops, one inside the other, with the variables $shelf and $book.
$targetBook and set it to 3 before the loops.Define $targetBook before the loops to know which book to find.
if statement to check if $book equals $targetBook. If yes, use break 2; to exit both loops.Use break 2; to stop both loops when the target book is found.
print statement to display: "Found book $targetBook on shelf $shelf".Use print to show the message with the variables $targetBook and $shelf.
What does the break statement do in PHP loops?
break statement is used to exit a loop early when a condition is met.Which of the following is the correct syntax to break out of 2 nested loops in PHP?
for (...) {
for (...) {
// break here
}
}break n; where n is the number of loops to break out of.break 2; correctly breaks out of 2 loops. break(2); is invalid syntax. A plain break; or break 1; breaks only one loop.What is the output of this PHP code?
$count = 0;
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$count++;
if ($i + $j == 2) {
break 2;
}
}
}
echo $count;Find the error in this PHP code snippet:
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($i == $j) {
break 3;
}
echo "$i,$j\n";
}
}break 3; tries to break out of 3 loops, but only 2 exist, causing a fatal error.You want to stop processing nested loops early when a condition is met, but only exit the inner loop sometimes and both loops other times. Which code snippet correctly uses break with levels?
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
if ($i == 2 && $j == 3) {
break 2; // exit both loops
} elseif ($j == 4) {
break; // exit inner loop only
}
echo "$i,$j\n";
}
}break 2; exits two loops, break; exits only the current loop.$i==2 && $j==3, both loops exit with break 2;. When $j==4, only inner loop exits with break;. This matches the requirement.Pass the test and this pattern moves unknown