PHP If-Else Statements Practice Questions with Solutions

Introductions

The if-else statement in PHP allows a program to choose between two different actions. If a condition is true, PHP executes the if block; otherwise, it executes the else block. For example, you can use if-else to check whether a student has passed or failed, whether an employee qualifies for a bonus, or whether a product is available in stock. PHP If-Else Statements Practice Questions with Solutions to help you understand the concepts.

Question 1: Check Whether a Number Is Positive or Not

Problem Statement:
Write a PHP program to check whether a number is positive. If the number is greater than zero, display a positive message; otherwise, display a message that the number is not positive.

PHP Solution:

<?php
$number = 15;

if ($number > 0) {
    echo "The number is positive.";
} else {
    echo "The number is not positive.";
}
?>

Sample Output:

The number is positive.

Explanation:
The condition $number > 0 checks whether the number is greater than zero. If the condition is true, PHP executes the if block. Otherwise, it executes the else block.

Concepts Covered: if-else statement, comparison operators, and conditional execution.

Question 2: Check Whether a Student Has Passed or Failed

Problem Statement:
A student passes an examination if their marks are 40 or higher. Write a PHP program to display whether the student has passed or failed.

PHP Solution:

<?php
$studentName = "Rahul";
$marks = 35;

if ($marks >= 40) {
    echo $studentName . " has passed the examination.";
} else {
    echo $studentName . " has failed the examination.";
}
?>

Sample Output:

Rahul has failed the examination.

Explanation:
The condition $marks >= 40 checks whether the student has scored at least 40 marks. Since Rahul scored 35, the condition is false, and PHP executes the else block.

Concepts Covered: if-else, comparison operators, student results, and string concatenation.

Question 3: Check Whether a Person Is Eligible to Vote

Problem Statement:
A person must be at least 18 years old to be eligible to vote. Write a PHP program to display the appropriate message based on the person’s age.

PHP Solution:

<?php
$age = 16;

if ($age >= 18) {
    echo "You are eligible to vote.";
} else {
    echo "You are not eligible to vote.";
}
?>

Sample Output:

You are not eligible to vote.

Explanation:
PHP checks whether $age is greater than or equal to 18. Since the person’s age is 16, the condition is false, and the else block displays the message.

Concepts Covered: if-else, age checking, and comparison operators.

Question 4: Check Whether a Number Is Even or Odd

Problem Statement:
Write a PHP program to check whether a number is even or odd using an if-else statement.

PHP Solution:

<?php
$number = 17;

if ($number % 2 == 0) {
    echo $number . " is an even number.";
} else {
    echo $number . " is an odd number.";
}
?>

Sample Output:

17 is an odd number.

Explanation:
The modulus operator (%) returns the remainder after division. If a number divided by 2 leaves a remainder of zero, it is even. Otherwise, it is odd.

Since 17 % 2 returns 1, PHP executes the else block.

Concepts Covered: if-else, modulus operator, equality operator, and number checking.

Question 5: Check Whether a Customer Gets a Discount

Problem Statement:
A shop offers a discount to customers whose purchase amount is ₹5,000 or more. Write a PHP program to display whether a customer qualifies for the discount.

PHP Solution:

<?php
$purchaseAmount = 3500;

if ($purchaseAmount >= 5000) {
    echo "Congratulations! You qualify for a discount.";
} else {
    echo "Sorry! You do not qualify for a discount.";
}
?>

Sample Output:

Sorry! You do not qualify for a discount.

Explanation:
The program checks whether the purchase amount is at least ₹5,000. Because the purchase amount is ₹3,500, the condition is false, and the else block executes.

Concepts Covered: if-else, comparison operators, purchase amounts, and business conditions.

Question 6: Check Whether an Employee Is Eligible for a Bonus

Problem Statement:
A company provides a performance bonus to employees who achieve a sales target of ₹100,000 or more. Write a PHP program to check whether an employee qualifies for the bonus.

PHP Solution:

<?php
$employeeName = "Priya";
$sales = 125000;

if ($sales >= 100000) {
    echo $employeeName . " is eligible for the bonus.";
} else {
    echo $employeeName . " is not eligible for the bonus.";
}
?>

Sample Output:

Priya is eligible for the bonus.

Explanation:
The condition $sales >= 100000 checks whether the employee has achieved the sales target. Priya’s sales are ₹125,000, so the condition is true and the bonus eligibility message is displayed.

Concepts Covered: if-else, sales targets, comparison operators, and employee data.

Question 7: Check Whether a Product Is Available in Stock

Problem Statement:
An online store has a product quantity stored in a variable. Write a PHP program to display whether the product is available or out of stock.

PHP Solution:

<?php
$productName = "Wireless Mouse";
$stock = 0;

if ($stock > 0) {
    echo $productName . " is available in stock.";
} else {
    echo $productName . " is out of stock.";
}
?>

Sample Output:

Wireless Mouse is out of stock.

Explanation:
The condition $stock > 0 checks whether any units are available. Since the stock quantity is zero, the condition is false and the program displays the out-of-stock message.

Concepts Covered: if-else, inventory management, comparison operators, and string concatenation.

Question 8: Check Whether a Number Is Divisible by 5

Problem Statement:
Write a PHP program to check whether a number is divisible by 5 without leaving a remainder.

PHP Solution:

<?php
$number = 42;

if ($number % 5 == 0) {
    echo $number . " is divisible by 5.";
} else {
    echo $number . " is not divisible by 5.";
}
?>

Sample Output:

42 is not divisible by 5.

Explanation:
The expression $number % 5 calculates the remainder after dividing the number by 5. If the remainder is zero, the number is divisible by 5. Otherwise, it is not.

Since 42 % 5 returns 2, the program executes the else block.

Concepts Covered: if-else, modulus operator, equality operator, and divisibility.

Question 9: Compare Two Numbers

Problem Statement:
Write a PHP program to compare two numbers. If the first number is greater than the second, display that the first number is greater; otherwise, display that it is not greater.

PHP Solution:

<?php
$a = 25;
$b = 40;

if ($a > $b) {
    echo $a . " is greater than " . $b . ".";
} else {
    echo $a . " is not greater than " . $b . ".";
}
?>

Sample Output:

25 is not greater than 40.

Explanation:
The condition $a > $b checks whether the first number is greater than the second. Since 25 is less than 40, the condition is false, and the else block executes.

Notice that if both numbers were equal, the else block would also execute because the condition uses > rather than >=.

Concepts Covered: if-else, comparison operators, variables, and string concatenation.

Question 10: Check Whether a Student Has Scored at Least 75 Marks

Problem Statement:
A student receives a special achievement message if they score 75 marks or more. Write a PHP program to check the student’s marks and display the appropriate message.

PHP Solution:

<?php
$studentName = "Amit";
$marks = 82;

if ($marks >= 75) {
    echo "Congratulations, " . $studentName . "! You achieved the target.";
} else {
    echo $studentName . ", you did not achieve the target.";
}
?>

Sample Output:

Congratulations, Amit! You achieved the target.

Explanation:
The condition $marks >= 75 checks whether the student scored at least 75 marks. Amit scored 82, so the condition is true, and the achievement message is displayed.

If the student scored fewer than 75 marks, the else block would display the alternative message.

Concepts Covered: if-else statement, comparison operators, student marks, and conditional messages.


Key Takeaways

  • The if-else statement in PHP allows a PHP program to choose between two blocks of code.
  • The if block runs when its condition is true.
  • The else block runs when the if condition is false.
  • Comparison operators such as >, <, >=, <=, and == are frequently used in conditions.
  • The modulus operator (%) can help determine whether a number is even, odd, or divisible by another number.
  • You can use if-else statements in student result systems, employee management, inventory systems, and online stores.
  • Curly braces {} make the code structure clear and help prevent mistakes.
  • The else block does not have a condition of its own; it handles the alternative when the if condition is false.

Frequently Asked Questions (FAQs)

1. What is an if-else statement in PHP?

An if-else statement executes one block of code if a condition is true and another block if the condition is false.

2. What is the syntax of an if-else statement in PHP?

if (condition) {
    // Executes when the condition is true
} else {
    // Executes when the condition is false
}

3. What happens when an if condition is false?

PHP skips the if block and executes the else block.

4. Can an if-else statement check whether a number is even or odd?

Yes. Use $number % 2 == 0 to check whether a number is even. If the condition is false, the number is odd.

5. What is the difference between if and if-else?

An if statement executes code only when its condition is true. An if-else statement also provides an alternative block for when the condition is false.

6. Can I use variables inside an if-else condition?

Yes. You can use variables with comparison operators to check values such as age, marks, salary, and stock quantity.

7. Is the else condition written inside parentheses?

No. The else keyword does not require a condition or parentheses. It follows the closing brace of the if block.

8. Can I use multiple statements inside the if and else blocks?

Yes. Each block can contain multiple PHP statements enclosed in curly braces.

9. Can if-else statements be used in real-world applications?

Yes. They are useful for checking discounts, exam results, employee bonuses, login conditions, and product availability.

10. What is the difference between == and >= in an if-else statement?

The == operator checks whether two values are equal. The >= operator checks whether the left value is greater than or equal to the right value.


Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top