Why conditional flow is needed in PHP - Performance Analysis
We want to see how adding choices in code affects how long it takes to run.
Does checking conditions slow down the program as input grows?
Analyze the time complexity of the following code snippet.
function checkNumbers(array $numbers) {
foreach ($numbers as $num) {
if ($num % 2 === 0) {
echo "$num is even\n";
} else {
echo "$num is odd\n";
}
}
}
This code checks each number in a list and prints if it is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array.
- How many times: Once for every number in the list.
Each number is checked once, so work grows directly with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows evenly as the list gets bigger.
Time Complexity: O(n)
This means the program takes longer in direct proportion to how many numbers it checks.
[X] Wrong: "Adding an if-else inside the loop makes the program much slower, like squared time."
[OK] Correct: The if-else just picks one path per item; it doesn't add extra loops, so time still grows linearly.
Understanding how conditions inside loops affect time helps you explain your code clearly and shows you know how programs scale.
"What if we added another loop inside the if condition? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of conditional flow
Conditional flow allows a program to choose what code to run based on true or false conditions.Step 2: Compare options with this purpose
Only To make decisions and run different code based on conditions describes making decisions and running different code based on conditions, which matches the purpose.Final Answer:
To make decisions and run different code based on conditions -> Option CQuick Check:
Conditional flow = decision making [OK]
- Confusing conditional flow with loops
- Thinking conditional flow stores data
- Believing conditional flow creates variables
Solution
Step 1: Recall PHP if statement syntax
PHP requires parentheses around the condition and curly braces around the code block.Step 2: Check each option for correct syntax
if ($a > $b) { echo 'Yes'; } uses parentheses and curly braces correctly. if ($a > $b) echo 'Yes' misses semicolon after echo and curly braces, causing syntax error. The other options lack parentheses or use invalid 'then' keyword.Final Answer:
if ($a > $b) { echo 'Yes'; } -> Option AQuick Check:
Correct if syntax = if ($a > $b) { echo 'Yes'; } [OK]
- Omitting parentheses around condition
- Using 'then' keyword which PHP does not have
- Missing curly braces for code block
$score = 75;
if ($score >= 60) {
echo 'Pass';
} else {
echo 'Fail';
}Solution
Step 1: Evaluate the condition $score >= 60
The variable $score is 75, which is greater than or equal to 60, so the condition is true.Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing 'Pass'.Final Answer:
Pass -> Option AQuick Check:
75 >= 60 is true, so output = Pass [OK]
- Confusing true and false branches
- Assuming code errors without syntax issues
- Ignoring the else block
if $age > 18 {
echo 'Adult';
} else {
echo 'Minor';
}Solution
Step 1: Check syntax of if statement
PHP requires parentheses around the condition in an if statement.Step 2: Identify the missing parentheses
The code uses if $age > 18 without parentheses, which causes a syntax error.Final Answer:
Missing parentheses around the condition -> Option BQuick Check:
if condition needs parentheses [OK]
- Forgetting parentheses in if conditions
- Thinking semicolons are needed after echo inside braces
- Assuming else keyword is wrong
Solution
Step 1: Understand the conditions for each greeting
'Good morning' if hour < 12, 'Good afternoon' if 12 ≤ hour ≤ 17, else 'Good evening'.Step 2: Analyze the correct structure
if ($hour < 12) { echo 'Good morning'; } elseif ($hour <= 17) { echo 'Good afternoon'; } else { echo 'Good evening'; } works perfectly: elseif only runs if hour ≥ 12, and ≤ 17 covers 12 to 17 inclusive without gaps or overlaps.Step 3: Check other structures for errors
The structure with if ($hour <= 12) for morning misclassifies hour 12. The structure with separate if for afternoon followed by else prints both morning and evening for hour < 12. The structure with else if ($hour > 12 && $hour < 17) excludes 12 and 17 from afternoon.Final Answer:
if ($hour < 12) { echo 'Good morning'; } elseif ($hour <= 17) { echo 'Good afternoon'; } else { echo 'Good evening'; } -> Option DQuick Check:
if <12 morning, elseif ≤17 (implies ≥12) afternoon, else evening [OK]
- Overlapping or missing hour ranges
- Using else if instead of elseif (both work but watch syntax)
- Not including boundary values like 12 or 17 correctly
Ready to prove it?
Pass the test and this pattern moves unknown
