Conditional statements allow PHP to execute different blocks of code depending on whether a condition is true or false. They are commonly used in login systems, result calculations, eligibility checks, and other applications.
The if statement executes a block of code
only when its condition is true.
Syntax:
if (condition) {
// Code executes if the condition is true
}
Example:
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
?>
Output:
You are eligible to vote.
Since the age is 20, the condition
$age >= 18 is true, so PHP executes
the statement inside the if block.
The if-else statement provides two possible
paths. PHP executes the if block when
the condition is true; otherwise, it executes the
else block.
Syntax:
if (condition) {
// If condition is true
} else {
// If condition is false
}
Example:
<?php
$marks = 30;
if ($marks >= 40) {
echo "Pass";
} else {
echo "Fail";
}
?>
Output:
Fail
The marks are less than 40, so PHP executes the
else block.
Use elseif when you need to check
multiple conditions. PHP checks them in order and
executes the first matching block.
Syntax:
if (condition1) {
// First condition
} elseif (condition2) {
// Second condition
} else {
// No condition matched
}
Example:
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 60) {
echo "Grade B";
} elseif ($marks >= 40) {
echo "Grade C";
} else {
echo "Fail";
}
?>
Output:
Grade B
The first condition is false, but the second condition
is true. Therefore, PHP displays
Grade B and skips the remaining branches.
Conditional statements can identify whether a number is positive, negative, or zero.
<?php
$number = -5;
if ($number > 0) {
echo "Positive number";
} elseif ($number < 0) {
echo "Negative number";
} else {
echo "Zero";
}
?>
Output:
Negative number
PHP checks whether the number is greater than zero, less than zero, or equal to zero.
A nested if statement is an
if statement placed inside another
if statement.
Example:
<?php
$age = 22;
$hasId = true;
if ($age >= 18) {
if ($hasId) {
echo "Entry allowed";
} else {
echo "Please show your ID";
}
} else {
echo "You are too young";
}
?>
Output:
Entry allowed
The first condition checks the age. If it is true, the nested condition checks whether the person has an ID.
You can combine conditions using logical operators
such as && (AND) and
|| (OR).
Example using AND:
<?php
$age = 25;
$hasLicense = true;
if ($age >= 18 && $hasLicense) {
echo "You can drive.";
} else {
echo "You cannot drive.";
}
?>
Output:
You can drive.
Both conditions must be true for the AND operator to produce true.
Example using OR:
<?php
$day = "Sunday";
if ($day == "Saturday" || $day == "Sunday") {
echo "Holiday";
} else {
echo "Working day";
}
?>
Output:
Holiday
With OR, at least one condition must be true.
The modulus operator (%) returns the
remainder after division. An integer is even when
its remainder after division by 2 is zero.
<?php
$number = 12;
if ($number % 2 == 0) {
echo "Even number";
} else {
echo "Odd number";
}
?>
Output:
Even number
Try changing $number to 7 and run the
program again.
Output for 7:
Odd number
You can compare two values to find which one is larger.
<?php
$a = 50;
$b = 80;
if ($a > $b) {
echo "A is greater";
} elseif ($b > $a) {
echo "B is greater";
} else {
echo "Both numbers are equal";
}
?>
Output:
B is greater
The final else handles the case where
both numbers are equal.
PHP supports an alternative syntax for conditions, which is sometimes useful when mixing PHP with HTML.
Example:
<?php
$loggedIn = true;
?>
<?php if ($loggedIn): ?>
<h3>Welcome to your dashboard!</h3>
<?php else: ?>
<h3>Please log in.</h3>
<?php endif; ?>
Output:
Welcome to your dashboard!
Remember these rules:
elseif to check additional conditions.== for loose comparison and === for strict comparison.endif; to close the conditional block.Write PHP programs to complete these exercises:
Sample starting code:
<?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";
}
?>
Expected output:
Grade B
1. Which statement executes code only when a condition is true?
2. What will this code display if $marks = 30?
if ($marks >= 40) {
echo "Pass";
} else {
echo "Fail";
}
3. Which operator checks whether both conditions are true?