📖 Scenario: You are creating a simple PHP program that checks a person's age and prints a message based on their age group.
🎯 Goal: Build a PHP program that uses an elseif ladder to print different messages for different age ranges.
📋 What You'll Learn
Create a variable called age with a specific integer value.
Create a variable called message to store the output message.
Use an if-elseif-else ladder to assign the correct message to message based on age.
Print the message variable.
💡 Why This Matters
🌍 Real World
Checking age groups is common in websites for age-restricted content, membership categories, or personalized messages.
💼 Career
Understanding conditional statements like <code>if-elseif-else</code> is essential for decision-making logic in programming jobs.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to 25.
PHP
Hint
Use $age = 25; to create the variable.
2
Create the message variable
Create a variable called message and set it to an empty string "".
PHP
Hint
Use $message = ""; to create an empty string variable.
3
Write the if-elseif-else ladder
Use an if-elseif-else ladder with $age to set $message as follows: - If $age is less than 13, set $message to "You are a child." - Elseif $age is less than 20, set $message to "You are a teenager." - Elseif $age is less than 65, set $message to "You are an adult." - Else, set $message to "You are a senior."
PHP
Hint
Use if, elseif, and else to check the age ranges and assign the messages.
4
Print the message
Write a print statement to display the value of the message variable.
PHP
Hint
Use print($message); to show the message on the screen.
Practice
(1/5)
1.
What does an elseif ladder do in PHP?
easy
A. It only checks the last condition in the ladder.
B. It runs all conditions regardless of their truth value.
C. It checks conditions one by one and runs the first true block.
D. It skips all conditions and runs the else block always.
Solution
Step 1: Understand elseif ladder behavior
An elseif ladder evaluates conditions from top to bottom.
Step 2: Identify execution flow
It executes the code block of the first condition that is true and skips the rest.
Final Answer:
It checks conditions one by one and runs the first true block. -> Option C
Quick Check:
Elseif ladder = first true condition runs [OK]
Hint: Runs only first true condition block in order [OK]
Common Mistakes:
Thinking all conditions run
Believing it checks only last condition
Assuming else always runs
2.
Which of the following is the correct syntax for an elseif ladder in PHP?
In PHP, conditions must be inside parentheses, but here elseif lacks parentheses.
Step 2: Check other parts
Echo statements have semicolons, variable names are correct, else block exists.
Final Answer:
Missing parentheses around elseif condition -> Option A
Quick Check:
Parentheses required for elseif condition [OK]
Hint: Always put conditions in parentheses [OK]
Common Mistakes:
Forgetting parentheses in elseif
Confusing semicolon placement
Assuming else block is optional
5.
You want to assign a category based on age using an elseif ladder. Which code correctly assigns "Child" for age < 13, "Teen" for age between 13 and 19 inclusive, and "Adult" for age 20 or more?