What is the purpose of an if-else statement in PHP?
An if-else statement lets the program choose between two paths based on a condition. If the condition is true, it runs one block of code; if false, it runs another.
Click to reveal answer
beginner
How do you write a simple if-else statement in PHP?
Use if (condition) { ... } else { ... }. The code inside the if block runs if the condition is true; otherwise, the else block runs.
Click to reveal answer
beginner
What happens if the if condition is false and there is no else block?
If the condition is false and no else block exists, PHP skips the if block and continues running the rest of the code.
Click to reveal answer
intermediate
Can you have multiple conditions checked in PHP using if-else?
Yes, you can use elseif to check multiple conditions in order. PHP runs the block for the first true condition it finds.
Click to reveal answer
beginner
Why is if-else important in programming?
If-else lets programs make decisions, like choosing what to do next based on data or user input. It makes programs flexible and interactive.
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 A
Quick Check:
75 >= 60 is true, so output = Pass [OK]
Hint: Check if condition is true to pick if or else output [OK]
Common Mistakes:
Confusing true and false branches
Ignoring comparison operators
Expecting numeric output instead of string
4. Find the error in this PHP code snippet:
if ($age > 18)
echo "Adult";
else
echo "Minor"
medium
A. Missing parentheses around condition
B. Missing braces around if and else blocks
C. Missing semicolon after echo "Minor"
D. Incorrect comparison operator
Solution
Step 1: Check syntax for echo statements
Each echo statement must end with a semicolon in PHP.
Step 2: Identify missing semicolon
The echo "Minor" line is missing a semicolon at the end, causing a syntax error.
Final Answer:
Missing semicolon after echo "Minor" -> Option C
Quick Check:
Every statement needs semicolon [OK]
Hint: Always end echo statements with semicolon [OK]
Common Mistakes:
Forgetting semicolon after last statement
Thinking braces are always required
Misreading comparison operators
5. You want to write a PHP program that prints "Good morning" if the hour is less than 12, "Good afternoon" if the hour is between 12 and 17 (inclusive), and "Good evening" otherwise. Which code correctly implements this using if-else statements?
hard
A. if ($hour < 12) {
echo "Good morning";
} else if ($hour <= 17) {
echo "Good afternoon";
} else {
echo "Good evening";
}
B. if ($hour < 12) {
echo "Good morning";
} else if ($hour > 12 && $hour < 17) {
echo "Good afternoon";
} else {
echo "Good evening";
}
C. if ($hour <= 12) {
echo "Good morning";
} else if ($hour >= 12 && $hour <= 17) {
echo "Good afternoon";
} else {
echo "Good evening";
}
D. if ($hour < 12) {
echo "Good morning";
} else if ($hour >= 12 && $hour < 17) {
echo "Good afternoon";
} else {
echo "Good evening";
}
Solution
Step 1: Analyze the conditions for each greeting
"Good morning" if hour < 12, "Good afternoon" if hour between 12 and 17 inclusive, else "Good evening".
Step 2: Check each option's condition ranges
if ($hour < 12) {
echo "Good morning";
} else if ($hour <= 17) {
echo "Good afternoon";
} else {
echo "Good evening";
} correctly uses $hour < 12, then $hour <= 17 for afternoon, covering 12 to 17 inclusive. Others either exclude 12 or 17 or overlap incorrectly.
Final Answer:
if ($hour < 12) {
echo "Good morning";
} else if ($hour <= 17) {
echo "Good afternoon";
} else {
echo "Good evening";
} -> Option A
Quick Check:
Correct ranges cover all hours without gaps [OK]
Hint: Check boundary conditions carefully in if-else chains [OK]
Common Mistakes:
Using wrong comparison operators for boundaries
Overlapping or missing hour ranges
Confusing inclusive and exclusive conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.