Introductions
PHP if-elseif-else statements allow a program to check multiple conditions and choose the appropriate action. They are useful when two outcomes are not enough. For example, a student may receive a grade based on marks, an employee may receive a bonus based on performance, or a shop may offer different discounts based on purchase amounts. PHP If-Elseif-Else statements practice questions with solutions to help you understand the concepts.
Question 1: Display a Student’s Grade Based on Marks
Problem Statement:
Write a PHP program to display a student’s grade according to these rules:
- 90 or above: Grade A
- 75 or above: Grade B
- 60 or above: Grade C
- 40 or above: Grade D
- Below 40: Fail
PHP Solution:
<?php
$marks = 82;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 75) {
echo "Grade B";
} elseif ($marks >= 60) {
echo "Grade C";
} elseif ($marks >= 40) {
echo "Grade D";
} else {
echo "Fail";
}
?>
Sample Output:
Grade B
Explanation:
PHP checks the conditions from top to bottom. Since 82 is less than 90 but greater than or equal to 75, the second condition is true, and Grade B is displayed. PHP skips the remaining branches after finding the first true condition.
Concepts Covered: if-elseif-else in PHP, comparison operators, grading systems, and conditional execution.
Question 2: Calculate an Electricity Bill Category
Problem Statement:
Write a PHP program to display an electricity usage category based on monthly units consumed:
- Up to 100 units: Low Usage
- Up to 200 units: Moderate Usage
- Up to 300 units: High Usage
- Above 300 units: Very High Usage
Assume the number of units cannot be negative.
PHP Solution:
<?php
$units = 250;
if ($units < 0) {
echo "Invalid unit consumption";
} elseif ($units <= 100) {
echo "Low Usage";
} elseif ($units <= 200) {
echo "Moderate Usage";
} elseif ($units <= 300) {
echo "High Usage";
} else {
echo "Very High Usage";
}
?>
Sample Output:
High Usage
Explanation:
The program checks each range in order. Since 250 is greater than 200 but does not exceed 300, the program displays High Usage.
The first condition also prevents negative unit values from being classified as a valid usage category.
Concepts Covered: Multiple conditions, range checking, comparison operators, and input validation logic.
Question 3: Calculate an Employee’s Performance Rating
Problem Statement:
A company rates employees based on their performance score:
- 90–100: Excellent
- 75–89: Very Good
- 60–74: Good
- 40–59: Needs Improvement
- Below 40: Poor
Write a PHP program to display the employee’s rating. Assume the score must be between 0 and 100.
PHP Solution:
<?php
$employeeName = "Rahul";
$score = 88;
if ($score < 0 || $score > 100) {
echo "Invalid performance score";
} elseif ($score >= 90) {
echo $employeeName . ": Excellent";
} elseif ($score >= 75) {
echo $employeeName . ": Very Good";
} elseif ($score >= 60) {
echo $employeeName . ": Good";
} elseif ($score >= 40) {
echo $employeeName . ": Needs Improvement";
} else {
echo $employeeName . ": Poor";
}
?>
Sample Output:
Rahul: Very Good
Explanation:
The program first checks whether the score is outside the valid range. If it is valid, PHP checks the performance categories from highest to lowest. Rahul’s score of 88 qualifies for Very Good.
Concepts Covered: if-elseif-else in PHP, logical OR (||), score validation, and performance evaluation.
Question 4: Calculate a Shopping Discount
Problem Statement:
A shop provides discounts based on the purchase amount:
- ₹10,000 or more: 20% discount
- ₹5,000 or more: 10% discount
- ₹2,000 or more: 5% discount
- Below ₹2,000: No discount
Write a PHP program to calculate and display the discount and final amount.
PHP Solution:
<?php
$purchaseAmount = 8000;
if ($purchaseAmount < 0) {
echo "Invalid purchase amount";
} elseif ($purchaseAmount >= 10000) {
$discount = $purchaseAmount * 0.20;
} elseif ($purchaseAmount >= 5000) {
$discount = $purchaseAmount * 0.10;
} elseif ($purchaseAmount >= 2000) {
$discount = $purchaseAmount * 0.05;
} else {
$discount = 0;
}
if ($purchaseAmount >= 0) {
$finalAmount = $purchaseAmount - $discount;
echo "Purchase Amount: Rs. " . $purchaseAmount . "<br>";
echo "Discount: Rs. " . $discount . "<br>";
echo "Final Amount: Rs. " . $finalAmount;
}
?>
Sample Output:
Purchase Amount: Rs. 8000
Discount: Rs. 800
Final Amount: Rs. 7200
Explanation:
The program checks the purchase amount against the discount thresholds. Since ₹8,000 is at least ₹5,000 but less than ₹10,000, the customer receives a 10% discount.
The discount is ₹800, so the final amount is ₹8,000 − ₹800 = ₹7,200.
Concepts Covered: Multiple conditions, percentage calculations, arithmetic operators, and business rules.
Question 5: Display a Message Based on Age
Problem Statement:
Write a PHP program to classify a person into one of these age groups:
- Below 13: Child
- 13–19: Teenager
- 20–59: Adult
- 60 or above: Senior Citizen
Display an appropriate message based on the person’s age.
PHP Solution:
<?php
$age = 16;
if ($age < 0) {
echo "Invalid age";
} elseif ($age < 13) {
echo "You are a Child";
} elseif ($age <= 19) {
echo "You are a Teenager";
} elseif ($age <= 59) {
echo "You are an Adult";
} else {
echo "You are a Senior Citizen";
}
?>
Sample Output:
You are a Teenager
Explanation:
PHP checks the age groups in order. Since 16 is greater than 12 and no more than 19, the program displays You are a Teenager.
The first condition prevents a negative age from being classified into an age group.
Concepts Covered: if-elseif-else in PHP, range checking, age classification, and validation.
Question 6: Calculate Delivery Charges Based on Order Amount
Problem Statement:
An online store calculates delivery charges using these rules:
- ₹5,000 or more: Free Delivery
- ₹2,000 or more: ₹50 delivery charge
- Below ₹2,000: ₹100 delivery charge
Write a PHP program to calculate the delivery charge and final order amount.
PHP Solution:
<?php
$orderAmount = 1500;
if ($orderAmount < 0) {
echo "Invalid order amount";
} elseif ($orderAmount >= 5000) {
$deliveryCharge = 0;
} elseif ($orderAmount >= 2000) {
$deliveryCharge = 50;
} else {
$deliveryCharge = 100;
}
if ($orderAmount >= 0) {
$finalAmount = $orderAmount + $deliveryCharge;
echo "Order Amount: Rs. " . $orderAmount . "<br>";
echo "Delivery Charge: Rs. " . $deliveryCharge . "<br>";
echo "Final Amount: Rs. " . $finalAmount;
}
?>
Sample Output:
Order Amount: Rs. 1500
Delivery Charge: Rs. 100
Final Amount: Rs. 1600
Explanation:
The order amount is ₹1,500, which is below ₹2,000. Therefore, the delivery charge is ₹100. The program adds this charge to the order amount to calculate the final total of ₹1,600.
Concepts Covered: Multiple conditions, delivery charges, arithmetic calculations, and e-commerce logic.
Question 7: Calculate a Salary Bonus Based on Years of Service
Problem Statement:
A company provides employee bonuses according to years of service:
- 10 years or more: 15% bonus
- 5 years or more: 10% bonus
- 2 years or more: 5% bonus
- Less than 2 years: No bonus
Write a PHP program to calculate the bonus amount and total salary.
PHP Solution:
<?php
$employeeName = "Priya";
$salary = 40000;
$yearsOfService = 6;
if ($salary < 0 || $yearsOfService < 0) {
echo "Invalid employee details";
} elseif ($yearsOfService >= 10) {
$bonus = $salary * 0.15;
} elseif ($yearsOfService >= 5) {
$bonus = $salary * 0.10;
} elseif ($yearsOfService >= 2) {
$bonus = $salary * 0.05;
} else {
$bonus = 0;
}
if ($salary >= 0 && $yearsOfService >= 0) {
$totalSalary = $salary + $bonus;
echo "Employee: " . $employeeName . "<br>";
echo "Bonus: Rs. " . $bonus . "<br>";
echo "Total Salary: Rs. " . $totalSalary;
}
?>
Sample Output:
Employee: Priya
Bonus: Rs. 4000
Total Salary: Rs. 44000
Explanation:
Priya has completed six years of service, so she qualifies for a 10% bonus. Ten percent of ₹40,000 is ₹4,000. Adding the bonus to her salary gives a total of ₹44,000.
Concepts Covered: if-elseif-else in PHP, percentage calculations, logical operators, and employee salary management.
Question 8: Display a Message Based on Temperature
Problem Statement:
Write a PHP program to classify the temperature using these categories:
- Below 0°C: Freezing
- 0°C–15°C: Cold
- Above 15°C–25°C: Pleasant
- Above 25°C–35°C: Hot
- Above 35°C: Very Hot
PHP Solution:
<?php
$temperature = 28;
if ($temperature < 0) {
echo "Freezing Weather";
} elseif ($temperature <= 15) {
echo "Cold Weather";
} elseif ($temperature <= 25) {
echo "Pleasant Weather";
} elseif ($temperature <= 35) {
echo "Hot Weather";
} else {
echo "Very Hot Weather";
}
?>
Sample Output:
Hot Weather
Explanation:
The temperature is 28°C. It is greater than 25°C but not greater than 35°C, so the program displays Hot Weather.
Concepts Covered: Multiple conditions, range checking, comparison operators, and classification.
Question 9: Calculate Income Tax Category
Problem Statement:
Write a PHP program to classify an annual income into the following illustrative categories:
- Below ₹3,00,000: Low Income Category
- ₹3,00,000 to below ₹6,00,000: Middle Income Category
- ₹6,00,000 to below ₹10,00,000: Upper-Middle Income Category
- ₹10,00,000 or more: High Income Category
These are practice categories only, not official tax slabs.
PHP Solution:
<?php
$annualIncome = 750000;
if ($annualIncome < 0) {
echo "Invalid annual income";
} elseif ($annualIncome < 300000) {
echo "Low Income Category";
} elseif ($annualIncome < 600000) {
echo "Middle Income Category";
} elseif ($annualIncome < 1000000) {
echo "Upper-Middle Income Category";
} else {
echo "High Income Category";
}
?>
Sample Output:
Upper-Middle Income Category
Explanation:
The annual income is ₹7,50,000. It is at least ₹6,00,000 but less than ₹10,00,000, so the program displays Upper-Middle Income Category.
Concepts Covered: if-elseif-else in PHP, range checking, income classification, and business rules.
Question 10: Calculate a Student’s Grade and Result
Problem Statement:
A school uses the following grading rules:
- 90–100: Grade A
- 80–89: Grade B
- 70–79: Grade C
- 60–69: Grade D
- Below 60: Fail
Write a PHP program to validate a student’s marks and display the grade.
PHP Solution:
<?php
$studentName = "Aman";
$marks = 76;
if ($marks < 0 || $marks > 100) {
echo "Invalid marks";
} elseif ($marks >= 90) {
echo $studentName . " received Grade A";
} elseif ($marks >= 80) {
echo $studentName . " received Grade B";
} elseif ($marks >= 70) {
echo $studentName . " received Grade C";
} elseif ($marks >= 60) {
echo $studentName . " received Grade D";
} else {
echo $studentName . " has failed";
}
?>
Sample Output:
Aman received Grade C
Explanation:
The program first checks whether the marks are within the valid range of 0 to 100. Since Aman’s marks are 76, the condition for Grade C is the first grade condition that evaluates to true. PHP displays the corresponding message.
Concepts Covered: if-elseif-else in PHP, validation, comparison operators, and grading systems.
Key Takeaways
- The
if-elseif-elsestructure is used when a program must check multiple conditions. - PHP evaluates conditions from top to bottom.
- The first condition that evaluates to
truedetermines which block executes. - The
elseblock runs when none of the preceding conditions is true. - Arrange range-based conditions carefully, usually from highest to lowest or lowest to highest.
- Use comparison operators such as
>=,<, and>to define ranges. - Logical operators such as
&&and||can combine conditions. - Validate inputs before processing them to avoid incorrect classifications.
- This structure is useful for grading systems, discounts, salary bonuses, delivery charges, and other business applications.
Frequently Asked Questions (FAQs)
1. What is an if-elseif-else statement in PHP?
It is a conditional structure that checks multiple conditions and executes the block associated with the first condition that evaluates to true.
2. What is the syntax of an if-elseif-else statement?
if (condition1) {
// First condition is true
} elseif (condition2) {
// Second condition is true
} else {
// No preceding condition is true
}
3. What is the difference between elseif and else?
elseif checks another condition. else does not check a condition; it runs when all preceding conditions are false.
4. Can I use multiple elseif blocks in PHP?
Yes. You can use multiple elseif blocks to check as many alternative conditions as your program requires.
5. Does PHP check every condition in an if-elseif-else structure?
No. Once a condition evaluates to true and its block executes, PHP skips the remaining branches in that structure.
6. Why should conditions be arranged carefully?
If a broad condition appears before a more specific one, it may match first and prevent later conditions from running. Arrange conditions so that every intended category can be reached.
7. Can I use logical operators in elseif conditions?
Yes. Operators such as && (AND) and || (OR) allow you to combine multiple conditions.
8. Can if-elseif-else statements calculate values?
Yes. They can select percentages, calculate discounts, assign grades, determine bonuses, and perform other calculations based on conditions.
9. What happens if no condition is true and there is no else block?
PHP skips all the conditional blocks and continues with the next statement after the structure.
10. What is the difference between separate if statements and an if-elseif-else structure?
Separate if statements are evaluated independently, so more than one block may execute. In an if-elseif-else structure, only the first matching branch executes.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
