6. Conditional Statements (if, else, elseif, switch).

Conditional statements, including if, else, and elseif, are essential components of programming languages like PHP. They allow programmers to control the flow of execution in a program based on certain conditions.

Why we need them:

Conditional statements are necessary because they enable programs to make decisions and execute different blocks of code based on whether certain conditions are true or false. Without conditional statements, programs would execute in a linear fashion without any ability to adapt to different situations or inputs.

Conditional Statements in PHP:

  • if statement: Executes a block of code if a specified condition is true.
  • else statement: Executes a block of code if the condition in the if statement is false.
  • elseif statement: Executes a block of code if the previous conditions in the if statement are false, and the condition specified in the elseif statement is true.
  • switch statement: for handling multiple conditions in a more concise way.

if Statement:

  • The if statement is a conditional statement used to execute a block of code only if a specified condition is true.
  • Syntax:
if (condition) {
    // Code block to execute if the condition is true
}

Example:

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

else Statement:

  • The else statement is used in conjunction with the if statement to execute a block of code if the if condition is false.
  • Syntax:

Here’s a single code example demonstrating the use of the elseif statement in PHP:

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

Example:

$age = 15;
if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";
}

In summary, the if statement allows you to execute code conditionally based on whether a certain condition evaluates to true, while the else statement provides an alternative code block to execute when the condition is false. Together, they allow for decision-making within your PHP code.

elseif Statement:

  • The elseif statement is used in conjunction with the if statement to check additional conditions if the original if condition is false.
  • It allows you to specify multiple conditions to be evaluated sequentially, providing an alternative code block to execute when each condition is true.
  • The elseif statement must follow an if statement and precede the else statement (if present).
  • Syntax:
if (condition1) {
    // Code block to execute if condition1 is true
} elseif (condition2) {
    // Code block to execute if condition2 is true
} elseif (condition3) {
    // Code block to execute if condition3 is true
}

Example:

<?php
$grade = 85;

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

In this example:

  • We have a variable $grade representing a student’s score.
  • The if statement checks if the grade is greater than or equal to 90. If true, it outputs “Grade: A”.
  • If the condition in the if statement is false, it moves to the elseif statement.
  • The elseif statements check the grade against different ranges (80-89, 70-79, 60-69) and output the corresponding grade.
  • If none of the conditions are met, it executes the else block, outputting “Grade: F”.

This demonstrates how the elseif statement is used to handle multiple conditions sequentially in PHP.

Switch Statement:

The switch statement is used to perform different actions based on different conditions. It evaluates an expression and compares it with multiple cases. Each case contains a value to compare with the expression. If a match is found, the corresponding block of code is executed.

Syntax:

switch (expression) {
    case value1:
        // Code block to execute if expression equals value1
        break;
    case value2:
        // Code block to execute if expression equals value2
        break;
    // Additional cases as needed
    default:
        // Code block to execute if none of the above cases match
}
  • switch: The keyword that starts the switch statement.
  • expression: The value or variable being evaluated.
  • case: Specifies a possible value of the expression.
  • value1, value2, etc.: The values that the expression is compared against in each case.
  • break: Terminates the switch statement. If omitted, execution will continue to the next case.
  • default: Specifies the code block to execute if none of the cases match the expression.

Example:

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "It's Monday!";
        break;
    case "Tuesday":
        echo "It's Tuesday!";
        break;
    case "Wednesday":
        echo "It's Wednesday!";
        break;
    case "Thursday":
        echo "It's Thursday!";
        break;
    case "Friday":
        echo "It's Friday!";
        break;
    default:
        echo "It's the weekend!";
}
?>

In this example:

  • We have a variable $day representing the current day of the week.
  • The switch statement evaluates the value of $day.
  • Each case specifies a possible value of $day, along with the corresponding action to take if that value matches.
  • The default case is executed if none of the cases match.

The switch statement provides a convenient way to handle multiple conditions with cleaner syntax compared to nested if statements when dealing with a large number of conditions.

When to elseif and when to switch

When writing clean and maintainable code, it’s essential to choose the appropriate control structure based on the specific situation. The elseif construct is typically used when you have a limited number of conditions to check sequentially after an initial if statement. It’s straightforward and easy to understand, especially when each condition depends on the outcome of the previous one. On the other hand, the switch statement is preferred when you have a single expression that you want to compare against multiple possible values. It provides a more concise and readable way to handle multiple conditions. Generally, if you’re dealing with a series of conditions that are mutually exclusive and involve the same variable or expression, switch is the preferred choice. However, if the conditions are independent of each other or involve different variables, elseif is more appropriate. The key is to choose the control structure that best reflects the logic of your code and enhances its readability and maintainability for future developers.