PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP switch case Statement

The switch statement is a control structure in PHP that allows you to perform different actions based on the value of a variable or expression. It is an alternative to using multiple if and elseif statements when you have a number of different possible values to check.

Here's the basic syntax of the switch statement:

switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    default:
        code to be executed if n is different from all labels;
}
  • n: The variable or expression to evaluate.
  • label1, label2, ..., labeln: The different values that n can be. If n equals one of these values, the corresponding code block is executed.
  • default: This is optional. If provided, this code block is executed if n doesn't match any of the labels.

Let's look at an example:

$color = "blue";

switch ($color) {
    case "red":
        echo "The color is red!";
        break;
    case "blue":
        echo "The color is blue!";
        break;
    case "green":
        echo "The color is green!";
        break;
    default:
        echo "Unknown color!";
}

In this example, because $color is "blue", the output will be "The color is blue!". If $color was "purple", which doesn't have a corresponding case, the default case would be executed, and the output would be "Unknown color!".

Remember to include a break statement at the end of each case to prevent the code from running into the next case (this is known as "falling through"). If you intentionally want to fall through to the next case, you can leave out the break.

The switch statement can make your code easier to read and understand when you have a large number of conditions to check. However, it's only suitable when you're checking a single variable or expression against multiple exact values. If you need to check multiple variables or use comparison operators (like < or >), you should use if and elseif instead.

  1. How to use switch case in PHP:

    <?php
    $day = "Monday";
    
    switch ($day) {
        case "Monday":
            echo "It's the start of the week!";
            break;
        case "Friday":
            echo "It's almost the weekend!";
            break;
        default:
            echo "It's a regular day.";
    }
    
  2. PHP switch case multiple conditions:

    <?php
    $fruit = "apple";
    $color = "red";
    
    switch (true) {
        case ($fruit == "apple" && $color == "red"):
            echo "It's a red apple!";
            break;
        case ($fruit == "banana" && $color == "yellow"):
            echo "It's a yellow banana!";
            break;
        default:
            echo "Unknown fruit or color combination.";
    }
    
  3. Switch case vs if-else in PHP:

    <?php
    $day = "Monday";
    
    if ($day == "Monday") {
        echo "It's the start of the week!";
    } elseif ($day == "Friday") {
        echo "It's almost the weekend!";
    } else {
        echo "It's a regular day.";
    }
    
  4. Handling default cases in PHP switch statements:

    The default case is executed when none of the specified cases match:

    <?php
    $day = "Wednesday";
    
    switch ($day) {
        case "Monday":
            echo "It's the start of the week!";
            break;
        case "Friday":
            echo "It's almost the weekend!";
            break;
        default:
            echo "It's a regular day.";
    }
    
  5. Nested switch case statements in PHP:

    <?php
    $day = "Monday";
    $weather = "sunny";
    
    switch ($weather) {
        case "sunny":
            switch ($day) {
                case "Saturday":
                case "Sunday":
                    echo "It's a sunny weekend!";
                    break;
                default:
                    echo "It's a sunny day!";
            }
            break;
        default:
            echo "Weather conditions unknown.";
    }
    
  6. Using break and fallthrough in PHP switch case:

    <?php
    $number = 2;
    
    switch ($number) {
        case 1:
            echo "One";
            break;
        case 2:
            echo "Two";
            // no break, falls through to the next case
        case 3:
            echo "Three";
            break;
        default:
            echo "Unknown number.";
    }
    
  7. PHP switch case with string values:

    <?php
    $color = "red";
    
    switch ($color) {
        case "red":
            echo "It's a red color.";
            break;
        case "blue":
            echo "It's a blue color.";
            break;
        default:
            echo "Unknown color.";
    }
    
  8. Comparing switch case and array lookup in PHP:

    <?php
    $fruit = "apple";
    
    // Using switch case
    switch ($fruit) {
        case "apple":
            echo "It's an apple.";
            break;
        case "banana":
            echo "It's a banana.";
            break;
        default:
            echo "Unknown fruit.";
    }
    
    // Using array lookup
    $fruitMap = ["apple" => "It's an apple.", "banana" => "It's a banana."];
    echo $fruitMap[$fruit] ?? "Unknown fruit.";
    
  9. PHP switch case with multiple values:

    <?php
    $day = "Saturday";
    
    switch ($day) {
        case "Saturday":
        case "Sunday":
            echo "It's the weekend!";
            break;
        default:
            echo "It's a weekday.";
    }
    
  10. Switch case for handling different data types in PHP:

    <?php
    $value = "5";
    
    switch ($value) {
        case 5:
            echo "It's the number 5.";
            break;
        case "5":
            echo "It's the string '5'.";
            break;
        default:
            echo "Unknown value.";
    }
    
  11. Complex conditions in PHP switch case:

    <?php
    $day = "Monday";
    $weather = "sunny";
    
    switch (true) {
        case ($day == "Saturday" && $weather == "sunny"):
            echo "It's a sunny Saturday!";
            break;
        case ($day == "Sunday" && $weather == "sunny"):
            echo "It's a sunny Sunday!";
            break;
        default:
            echo "Not a sunny weekend day.";
    }
    
  12. Handling edge cases with switch case in PHP:

    <?php
    $number = 0;
    
    switch (true) {
        case ($number > 0):
            echo "Positive number.";
            break;
        case ($number < 0):
            echo "Negative number.";
            break;
        default:
            echo "Zero.";
    }