Break statement with levels in PHP - Time & Space Complexity
Let's see how using a break statement with levels affects how long a PHP program runs.
We want to know how the number of steps changes as the input grows.
Analyze the time complexity of the following code snippet.
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
if ($j == 2) {
break 2; // exit both loops
}
// some constant time operation
}
}
This code loops over two levels but breaks out of both loops early when $j reaches 2.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop runs but breaks early at $j == 2.
- How many times: The outer loop runs only once ($i = 0), and the inner loop runs 3 times (j=0,1,2) before
break 2exits both loops.
Because of the break 2, both loops exit after just a few operations, regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 steps |
| 100 | About 3 steps |
| 1000 | About 3 steps |
Pattern observation: The work is constant because the loops terminate early, independent of $n.
Time Complexity: O(1)
This means the program's running time stays constant as $n increases, thanks to the multilevel break stopping both loops early.
[X] Wrong: "Because there are two loops, the time must be O(n*m)."
[OK] Correct: The break 2 exits both loops after $j reaches 2 in the first outer iteration, so total work is constant, not O(n*m).
Understanding how break with levels changes loop behavior helps you explain code efficiency clearly and confidently in interviews.
What if the break statement was removed? How would the time complexity change?
Practice
What does the break statement do in PHP loops?
Solution
Step 1: Understand the purpose of break
Thebreakstatement is used to exit a loop early when a condition is met.Step 2: Compare options
The description "It stops the current loop immediately." correctly describes break. "It skips the current iteration..." describes continue. Restarting or pausing are incorrect.Final Answer:
It stops the current loop immediately. -> Option DQuick Check:
break stops loop [OK]
- Confusing break with continue
- Thinking break pauses instead of stops
- Assuming break restarts the loop
Which of the following is the correct syntax to break out of 2 nested loops in PHP?
for (...) {
for (...) {
// break here
}
}Solution
Step 1: Recall break syntax with levels
To exit multiple nested loops, PHP usesbreak n;where n is the number of loops to break out of.Step 2: Identify correct syntax
break 2;correctly breaks out of 2 loops.break(2);is invalid syntax. A plainbreak;orbreak 1;breaks only one loop.Final Answer:
break 2; -> Option CQuick Check:
break 2; exits 2 loops [OK]
- Using break(2) which is invalid syntax
- Forgetting to specify number for multiple loops
- Using break 1; when 2 loops need exit
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;Solution
Step 1: Trace nested loops and break condition
Loops run with i and j from 0 to 2. The break triggers when i + j == 2, breaking out of both loops immediately. count++ occurs before the check.Step 2: Simulate iterations
- i=0,j=0: count=1 (sum=0 !=2)
- i=0,j=1: count=2 (sum=1 !=2)
- i=0,j=2: count=3 (sum=2 ==2), break 2; loops stop
Total count = 3.Final Answer:
3 -> Option BQuick Check:
3 increments before break 2 exits both loops [OK]
- Counting increments after break condition
- Ignoring break level effect
- Assuming break only exits inner loop
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";
}
}Solution
Step 1: Count nested loops
There are 2 nested loops: outer and inner.Step 2: Check break level usage
Usingbreak 3;tries to break out of 3 loops, but only 2 exist, causing a fatal error.Final Answer:
break 3; is invalid because only 2 loops exist. -> Option AQuick Check:
break n must be ≤ nested loops count [OK]
- Using break level higher than loops
- Assuming break 3 exits 2 loops
- Ignoring PHP fatal error on invalid break level
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";
}
}Solution
Step 1: Understand break with levels usage
break 2;exits two loops,break;exits only the current loop.Step 2: Analyze code logic
When$i==2 && $j==3, both loops exit withbreak 2;. When$j==4, only inner loop exits withbreak;. This matches the requirement.Final Answer:
Correct use of break with levels to control loop exit. -> Option AQuick Check:
break n exits n loops, break exits current loop [OK]
- Confusing break 2; with break 1;
- Thinking break; exits all loops
- Misusing break levels causing logic errors
Ready to prove it?
Pass the test and this pattern moves unknown
