Nested conditional execution in PHP - Time & Space Complexity
We want to see how the time a program takes changes when it uses nested conditions.
Specifically, we ask: does adding more conditions inside each other make the program slower as input grows?
Analyze the time complexity of the following code snippet.
function checkNumber(int $num): string {
if ($num > 0) {
if ($num % 2 === 0) {
return "Positive even";
} else {
return "Positive odd";
}
} else {
return "Zero or negative";
}
}
This code checks if a number is positive and then if it is even or odd, using nested if statements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps here, only simple if checks.
- How many times: Each condition runs once per function call.
The number of steps does not increase with the size of the input number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
Pattern observation: The steps stay the same no matter how big the number is.
Time Complexity: O(1)
This means the program takes the same amount of time no matter the input size.
[X] Wrong: "Nested ifs always make the program slower as input grows."
[OK] Correct: Nested conditions just check one after another once; they do not repeat or grow with input size.
Understanding how nested conditions affect time helps you explain your code clearly and shows you know when complexity grows or stays steady.
"What if we added a loop inside the nested ifs? How would the time complexity change?"
Practice
What does nested conditional execution mean in PHP?
Solution
Step 1: Understand conditional statements
Conditional statements likeifandelselet the program choose actions based on conditions.Step 2: Define nested conditionals
Nested conditionals mean placing one conditional inside another to check multiple conditions step-by-step.Final Answer:
Using aniforelsestatement inside anotheriforelseblock -> Option DQuick Check:
Nested conditionals = condition inside condition [OK]
- Thinking multiple separate ifs are nested
- Confusing nested with chained conditions
- Ignoring the inner condition placement
Which of the following is the correct syntax for nested if statements in PHP?
<?php
if ($a > 0) {
if ($b < 10) {
echo "Inside nested if";
}
}
?>Solution
Step 1: Check braces usage
Both outer and innerifstatements have braces to define their blocks properly.Step 2: Verify condition syntax
Conditions use parentheses correctly and logical operators are valid.Final Answer:
Correct syntax with braces and proper nesting -> Option BQuick Check:
Proper braces and nesting = correct syntax [OK]
- Omitting braces causing syntax errors
- Misplacing parentheses in conditions
- Confusing elseif with nested if
What will be the output of the following PHP code?
<?php
$x = 5;
$y = 8;
if ($x > 3) {
if ($y < 10) {
echo "A";
} else {
echo "B";
}
} else {
echo "C";
}
?>Solution
Step 1: Evaluate outer if condition
$x = 5, which is greater than 3, so the outerifblock runs.Step 2: Evaluate inner if condition
$y = 8, which is less than 10, so the innerifblock runs and prints "A".Final Answer:
A -> Option CQuick Check:
5 > 3 and 8 < 10 = A [OK]
- Ignoring inner condition result
- Confusing else blocks
- Assuming else runs when condition is true
Find the error in this nested conditional PHP code:
<?php
$a = 10;
if ($a > 5)
if ($a < 15)
echo "Inside nested if";
else
echo "Outside nested if";
?>Solution
Step 1: Analyze if-else pairing without braces
Without braces, theelsepairs with the nearestif, which is the inner one.Step 2: Understand the problem
This can cause confusion or unexpected behavior because the outerifhas noelseblock explicitly.Final Answer:
Missing braces causing else to pair with wrong if -> Option AQuick Check:
Braces needed to clarify else pairing [OK]
- Assuming else pairs with outer if
- Ignoring indentation as syntax guide
- Missing semicolons (not the issue here)
Given the following PHP code, what will be the output?
<?php
$score = 75;
if ($score >= 90) {
echo "Grade A";
} else {
if ($score >= 75) {
if ($score >= 80) {
echo "Grade B+";
} else {
echo "Grade B";
}
} else {
echo "Grade C";
}
}
?>Solution
Step 1: Check first condition
$score = 75, which is not >= 90, so skip firstifblock.Step 2: Check second condition
$score >= 75 is true, so enter secondif.Step 3: Check nested condition inside second if
$score >= 80 is false (75 < 80), so executeelseand print "Grade B".Final Answer:
Grade B -> Option AQuick Check:
75 >= 75 but < 80 = Grade B [OK]
- Confusing >= 75 and >= 80 checks
- Skipping nested else block
- Assuming 75 is >= 80
Ready to prove it?
Pass the test and this pattern moves unknown
