PHP if...elseif...else

In PHP, conditional statements (if, if-else, if-else-if) allow you to execute different code blocks based on certain conditions. Understanding these constructs is essential for controlling the flow of your PHP scripts. Here's a detailed overview of each:

if Statement

The if statement executes a block of code if a specified condition evaluates to true.

Syntax:

php
if (condition) {
    // Code to execute if the condition is true
}


Example:

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


In this example, the message "You are an adult." will be printed because the condition $age >= 18 is true.

if-else Statement


The if-else statement allows you to execute one block of code if the condition is true, and another block if the condition is false.

Syntax:

php
if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}


Example:

php
$age = 16;
if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are not an adult.";
}


In this example, the message "You are not an adult." will be printed because the condition $age >= 18 is false.

if-else-if Statement


The if-else-if statement allows you to test multiple conditions sequentially. If the condition of the if statement is false, it checks the else-if conditions in order, and if none are true, it executes the else block.

Syntax:

php
if (condition1) {
    // Code to execute if condition1 is true
} elseif (condition2) {
    // Code to execute if condition1 is false and condition2 is true
} elseif (condition3) {
    // Code to execute if condition1 and condition2 are false, and condition3 is true
} else {
    // Code to execute if none of the above conditions are true
}


Example:

php
$score = 85;
if ($score >= 90) {
    echo "Grade: A";
} elseif ($score >= 80) {
    echo "Grade: B";
} elseif ($score >= 70) {
    echo "Grade: C";
} elseif ($score >= 60) {
    echo "Grade: D";
} else {
    echo "Grade: F";
}


In this example, the message "Grade: B" will be printed because the condition $score >= 80 is true, and the conditions above it are false.

Combined Example


Here’s a combined example showing how you might use if, if-else, and if-else-if together:

php
$time = 14; // 2 PM in 24-hour format
$day = "Saturday";
if ($time < 12>    echo "Good morning!";
} elseif ($time < 18>    echo "Good afternoon!";
} else {
    echo "Good evening!";
}
if ($day == "Saturday" || $day == "Sunday") {
    echo " It's the weekend!";
} else {
    echo " It's a weekday.";
}


In this example:

    The first block determines if it is morning, afternoon, or evening based on the $time variable.
    The second block determines if it is the weekend or a weekday based on the $day variable.

Summary


    if Statement: Executes code if the condition is true.
    if-else Statement: Executes one block of code if the condition is true and another block if the condition is false.
    if-else-if Statement: Checks multiple conditions in sequence and executes the code block corresponding to the first true condition, or the else block if none of the conditions are true.

These constructs are fundamental for controlling the flow of your PHP scripts and making decisions based on different conditions.




  • To Share this Link, Choose your plateform


Our Other Tutorials