Introductions
The if statement is one of the most important concepts in PHP programming. It allows a program to make decisions by checking whether a condition is true. For example, you can use an if statement to check whether a student has passed an exam, whether a customer is eligible for a discount, or whether an employee meets a salary requirement. In this chapter, you will practice PHP questions to understand how if statements work in real-world situations. PHP If Statements Practice Questions with Solutions to help you understand the concepts.
Question 1: Check Whether a Number Is Positive
Problem Statement:
Write a PHP program to check whether a number is positive. Display a message only when the number is greater than zero.
PHP Solution:
<?php
$number = 15;
if ($number > 0) {
echo "The number is positive.";
}
?>
Sample Output:
The number is positive.
Explanation:
The if statement checks whether $number > 0 is true. Because 15 is greater than zero, PHP executes the code inside the curly braces and displays the message.
If the number were zero or negative, the condition would be false, and nothing would be displayed.
Concepts Covered: if statement, comparison operator, condition checking, and output.
Question 2: Check Whether a Student Has Passed
Problem Statement:
A student passes an exam if their marks are 40 or higher. Write a PHP program that displays a pass message when the student meets this requirement.
PHP Solution:
<?php
$marks = 75;
if ($marks >= 40) {
echo "Congratulations! You have passed.";
}
?>
Sample Output:
Congratulations! You have passed.
Explanation:
The condition $marks >= 40 checks whether the student’s marks are greater than or equal to 40.
Since the student scored 75, the condition is true, and PHP displays the pass message.
Concepts Covered: if statement, greater-than-or-equal operator (>=), and student result checking.
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 that displays a message if the person meets this age requirement.
PHP Solution:
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
?>
Sample Output:
You are eligible to vote.
Explanation:
PHP checks whether the person’s age is at least 18.
Because $age is 20, the condition is true, and the message is displayed.
If the age were 16, the condition would be false, so the program would not display the message.
Concepts Covered: if statement, comparison operators, and age-based decisions.
Question 4: Check Whether a Number Is Even
Problem Statement:
Write a PHP program that displays a message if a number is even.
PHP Solution:
<?php
$number = 24;
if ($number % 2 == 0) {
echo "The number is even.";
}
?>
Sample Output:
The number is even.
Explanation:
The modulus operator (%) calculates the remainder after division.
When an even number is divided by 2, the remainder is zero. Therefore, the condition $number % 2 == 0 is true for 24.
Concepts Covered: if statement, modulus operator (%), equality operator (==), and even-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 that displays a discount message when the customer qualifies.
PHP Solution:
<?php
$purchaseAmount = 6500;
if ($purchaseAmount >= 5000) {
echo "Congratulations! You qualify for a discount.";
}
?>
Sample Output:
Congratulations! You qualify for a discount.
Explanation:
The program checks whether the purchase amount is at least ₹5,000.
Because the customer spends ₹6,500, the condition is true, and the discount message appears.
This type of condition can be used in shopping websites and billing systems.
Concepts Covered: if statement, comparison operators, purchase calculations, and business logic.
Question 6: Check Whether an Employee’s Salary Exceeds ₹30,000
Problem Statement:
A company wants to identify employees earning more than ₹30,000 per month. Write a PHP program that displays a message if an employee’s salary exceeds this amount.
PHP Solution:
<?php
$employeeName = "Rahul";
$salary = 45000;
if ($salary > 30000) {
echo $employeeName . " earns more than Rs. 30,000.";
}
?>
Sample Output:
Rahul earns more than Rs. 30,000.
Explanation:
The condition $salary > 30000 checks whether the employee’s salary is strictly greater than ₹30,000.
Since Rahul earns ₹45,000, the condition is true. PHP then combines the employee’s name with the message using the concatenation operator (.).
A salary of exactly ₹30,000 would not satisfy this condition.
Concepts Covered: if statement, comparison operators, string concatenation, and employee data.
Question 7: Check Whether Stock Is Available
Problem Statement:
An online store should display a message when a product has at least one item in stock. Write a PHP program to check the available quantity.
PHP Solution:
<?php
$productName = "Keyboard";
$stock = 12;
if ($stock > 0) {
echo $productName . " is available in stock.";
}
?>
Sample Output:
Keyboard is available in stock.
Explanation:
The condition $stock > 0 checks whether at least one product is available.
Because the stock quantity is 12, PHP displays the availability message.
If the stock quantity were zero, the condition would be false, and this message would not appear.
Concepts Covered: if statement, comparison operators, inventory management, and concatenation.
Question 8: Check Whether a Student Has Scored Full Marks
Problem Statement:
An exam has a maximum score of 100. Write a PHP program that displays a special message when a student scores exactly 100 marks.
PHP Solution:
<?php
$studentName = "Priya";
$marks = 100;
if ($marks == 100) {
echo "Excellent, " . $studentName . "! You scored full marks.";
}
?>
Sample Output:
Excellent, Priya! You scored full marks.
Explanation:
The equality operator (==) checks whether the marks are equal to 100.
Because Priya scored exactly 100, the condition is true, and PHP displays the congratulatory message.
If she scored 99, the condition would be false.
Concepts Covered: if statement, equality operator (==), student marks, and string concatenation.
Question 9: Check Whether a Number Is Divisible by 5
Problem Statement:
Write a PHP program that checks whether a number is divisible by 5. Display a message if the number is divisible by 5 without leaving a remainder.
PHP Solution:
<?php
$number = 45;
if ($number % 5 == 0) {
echo $number . " is divisible by 5.";
}
?>
Sample Output:
45 is divisible by 5.
Explanation:
The modulus operator (%) finds the remainder after division.
The expression $number % 5 == 0 is true when the number is divisible by 5 without a remainder.
Since 45 divided by 5 leaves a remainder of zero, PHP displays the message.
Concepts Covered: if statement, modulus operator, equality operator, and divisibility checking.
Question 10: Check Whether an Employee Is Eligible for a Bonus
Problem Statement:
A company considers employees eligible for a bonus if they have completed at least 5 years of service. Write a PHP program that displays a bonus eligibility message when an employee meets this requirement.
PHP Solution:
<?php
$employeeName = "Amit";
$yearsOfService = 7;
if ($yearsOfService >= 5) {
echo $employeeName . " is eligible for a bonus.";
}
?>
Sample Output:
Amit is eligible for a bonus.
Explanation:
The condition $yearsOfService >= 5 checks whether the employee has completed five or more years of service.
Amit has completed seven years, so the condition is true, and the message is displayed.
This example demonstrates how companies can use conditions to check eligibility according to business rules.
Concepts Covered: if statement, comparison operators, employee records, and business logic.
Key Takeaways
- The
ifstatement allows PHP programs to make decisions based on conditions. - The code inside an
ifblock runs only when its condition evaluates totrue. - Curly braces
{}define the block of code associated with the condition. - Comparison operators such as
>,<,>=,<=, and==are commonly used withifstatements. - The modulus operator (
%) helps check whether numbers are even or divisible by another number. - You can combine variables and strings to display personalized messages.
ifstatements are useful in student result systems, employee management, online shopping, and inventory applications.- If a condition is false and no alternative branch is provided, PHP skips the code inside the
ifblock.
Frequently Asked Questions (FAQs)
1. What is an if statement in PHP?
An if statement executes a block of code when a specified condition is true.
2. What is the basic syntax of an if statement in PHP?
The basic syntax is:
if (condition) {
// Code runs when the condition is true
}
3. What happens when an if condition is false?
PHP skips the code inside the if block. If there is no alternative branch, execution continues with the next statement after the block.
4. Can I use comparison operators inside an if statement?
Yes. Operators such as >, <, >=, <=, ==, and === can be used to check conditions.
5. What is the difference between = and == in PHP?
The = operator assigns a value to a variable. The == operator compares two values for equality.
6. Can an if statement check whether a number is even?
Yes. You can use $number % 2 == 0 to check whether a number is even.
7. Can I use multiple statements inside an if block?
Yes. You can place multiple PHP statements inside the curly braces. All of them execute when the condition is true.
8. Are curly braces required in PHP if statements?
Curly braces are optional when the body contains only one statement, but using them is recommended for clarity and to avoid mistakes when the code changes.
9. Can an if statement be used for business applications?
Yes. It can check salary requirements, discount eligibility, stock availability, student results, and many other business conditions.
10. What is the difference between if and if-else?
An if statement executes code only when its condition is true. An if-else statement provides one block for a true condition and another block for a false condition.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
