Lesson 11 of 50 – PHP If, Else and Elseif
22%

PHP If, Else and Elseif

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.

1. The if Statement

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.

2. The if-else Statement

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.

3. The if-elseif-else Statement

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.

4. Checking Positive, Negative or Zero

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.

5. Nested if Statements

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.

6. Using Logical Operators in Conditions

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.

7. Checking Even or Odd Numbers

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
8. Checking the Largest of Two Numbers

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.

9. Alternative Syntax and Common Mistakes

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:

  • Use parentheses around the condition.
  • Use curly braces to group statements in the standard syntax.
  • Use elseif to check additional conditions.
  • Order multiple conditions carefully, from the most specific to the broader ones when appropriate.
  • Use == for loose comparison and === for strict comparison.
  • Alternative syntax uses endif; to close the conditional block.
📝 Practical Assignment

Write PHP programs to complete these exercises:

  1. Check whether a person is eligible to vote if their age is at least 18.
  2. Check whether a number is positive, negative, or zero.
  3. Check whether a number is even or odd.
  4. Display Pass if marks are at least 40; otherwise, display Fail.
  5. Display grades using these rules: 90 and above = A, 75–89 = B, 60–74 = C, 40–59 = D, below 40 = Fail.
  6. Find the largest of three numbers.
  7. Check whether a person can enter if they are at least 18 and have an ID.
  8. Check whether a day is a weekend using the OR operator.

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

🧠 Quick Quiz

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?