PHP If...Else...Elseif

Making decisions in your PHP code

🔀 What are Conditional Statements?

Conditional statements allow your code to make decisions and execute different code blocks based on conditions. The if...else...elseif structure helps control program flow based on true or false evaluations.


<?php
$age = 18;
if ($age >= 18) {
    echo "You are an adult!";
}
?>
                                    

Output:

You are an adult!

Conditional Statement Types

if Statement

Executes code block only if the specified condition evaluates to true, otherwise skips the block entirely.

<?php
if ($x > 0) {
    echo "Positive";
}
?>
↔️

if...else

Provides an alternative code block to execute when the if condition is false, ensuring one path is always taken.

<?php
if ($x > 0) {
    echo "Positive";
} else {
    echo "Not positive";
}
?>
🔄

if...elseif...else

Tests multiple conditions in sequence, executing the first matching condition's code block or the else block if none match.

<?php
if ($x > 0) {
    echo "Positive";
} elseif ($x < 0) {
    echo "Negative";
} else {
    echo "Zero";
}
?>
🎯

Nested if

Place if statements inside other if statements to create complex decision trees with multiple levels of conditions.

<?php
if ($x > 0) {
    if ($x > 10) {
        echo "Greater than 10";
    }
}
?>

🔹 Simple if Statement

Execute code only when a condition is true:

<?php
$temperature = 30;

if ($temperature > 25) {
    echo "It's hot outside!";
}
echo "<br>";

$isLoggedIn = true;
if ($isLoggedIn) {
    echo "Welcome back!";
}
echo "<br>";

$score = 85;
if ($score >= 80) {
    echo "Great job! You passed with distinction.";
}
?>

Output:

It's hot outside!

Welcome back!

Great job! You passed with distinction.

🔹 if...else Statement

Execute one block if true, another if false:

<?php
$age = 15;

if ($age >= 18) {
    echo "You can vote!";
} else {
    echo "You cannot vote yet.";
}
echo "<br><br>";

$hour = 14;
if ($hour < 12) {
    echo "Good morning!";
} else {
    echo "Good afternoon!";
}
echo "<br><br>";

$hasTicket = false;
if ($hasTicket) {
    echo "Enjoy the movie!";
} else {
    echo "Please buy a ticket first.";
}
?>

Output:

You cannot vote yet.


Good afternoon!


Please buy a ticket first.

🔹 if...elseif...else Statement

Test multiple conditions in sequence:

<?php
$score = 75;

if ($score >= 90) {
    echo "Grade: A - Excellent!";
} elseif ($score >= 80) {
    echo "Grade: B - Very Good!";
} elseif ($score >= 70) {
    echo "Grade: C - Good!";
} elseif ($score >= 60) {
    echo "Grade: D - Pass";
} else {
    echo "Grade: F - Fail";
}
echo "<br><br>";

$time = 20;
if ($time < 12) {
    echo "Good morning!";
} elseif ($time < 18) {
    echo "Good afternoon!";
} elseif ($time < 22) {
    echo "Good evening!";
} else {
    echo "Good night!";
}
?>

Output:

Grade: C - Good!


Good evening!

🔹 Nested if Statements

Place if statements inside other if statements:

<?php
$age = 25;
$hasLicense = true;

if ($age >= 18) {
    echo "You are old enough to drive.";
    echo "<br>";
    
    if ($hasLicense) {
        echo "You can drive!";
    } else {
        echo "But you need a license first.";
    }
} else {
    echo "You are too young to drive.";
}
echo "<br><br>";

$isWeekend = true;
$weather = "sunny";

if ($isWeekend) {
    if ($weather == "sunny") {
        echo "Perfect day for a picnic!";
    } else {
        echo "Weekend, but weather is not great.";
    }
} else {
    echo "It's a workday.";
}
?>

Output:

You are old enough to drive.

You can drive!


Perfect day for a picnic!

🔹 Combining Conditions

Use logical operators to combine multiple conditions:

<?php
$age = 25;
$income = 30000;

// AND operator (&&)
if ($age >= 18 && $income >= 25000) {
    echo "Loan approved!";
} else {
    echo "Loan denied.";
}
echo "<br><br>";

// OR operator (||)
$isMember = false;
$hasCoupon = true;

if ($isMember || $hasCoupon) {
    echo "You get a discount!";
} else {
    echo "No discount available.";
}
echo "<br><br>";

// Complex conditions
$temperature = 28;
$humidity = 70;

if ($temperature > 25 && $humidity > 60) {
    echo "It's hot and humid!";
} elseif ($temperature > 25) {
    echo "It's hot but dry.";
} else {
    echo "Weather is comfortable.";
}
?>

Output:

Loan approved!


You get a discount!


It's hot and humid!

🔹 Ternary Operator (Shorthand)

A shorter way to write simple if...else statements:

<?php
// Regular if...else
$age = 20;
if ($age >= 18) {
    $status = "Adult";
} else {
    $status = "Minor";
}
echo "Status: $status";
echo "<br>";

// Ternary operator (shorthand)
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
echo "Status: $status";
echo "<br><br>";

// More examples
$score = 85;
$result = ($score >= 60) ? "Pass" : "Fail";
echo "Result: $result";
echo "<br>";

$isLoggedIn = true;
echo $isLoggedIn ? "Welcome!" : "Please login";
?>

Output:

Status: Adult

Status: Adult


Result: Pass

Welcome!

🧠 Test Your Knowledge

What happens if all conditions in an if...elseif...else statement are false?