PHP PHP Tutorial PHP Forms PHP Advanced PHP OOP PHP MySQL Database PHP XML PHP - AJAX



PHP Switch

PHP switch statement is a control structure that allows you to execute different blocks of code based on the value of a variable or expression. It is similar to the if-else statement, but it provides a more concise and readable way to handle multiple conditions.

The switch statement evaluates an expression and compares it with a series of cases. If the expression matches a case, the corresponding block of code is executed. If none of the cases match, the default block of code is executed.

Here is the basic syntax of the switch statement:

switch (expression) {
    case value1:
        // code to be executed if expression matches value1
        break;
    case value2:
        // code to be executed if expression matches value2
        break;
    ...
    default:
        // code to be executed if none of the cases match
}

The expression can be any variable or expression that returns a value. The case values can be any constant or literal value, such as numbers, strings, or boolean values. The default block of code is optional, and it is executed if none of the cases match.

Let's see some examples of how to use the switch statement in PHP:

Example 1: Simple Switch Statement

In this example, we use a switch statement to determine the day of the week based on a numeric value:

$num = 3;

switch ($num) {
    case 1:
        echo "Monday";
        break;
    case 2:
        echo "Tuesday";
        break;
    case 3:
        echo "Wednesday";
        break;
    case 4:
        echo "Thursday";
        break;
    case 5:
        echo "Friday";
        break;
    case 6:
        echo "Saturday";
        break;
    case 7:
        echo "Sunday";
        break;
    default:
        echo "Invalid day";
}

The output of this code will be:

Wednesday

Example 2: Switch Statement with Multiple Cases

In this example, we use a switch statement to determine the discount percentage based on the total purchase amount:

$total = 100;

switch ($total) {
    case $total >= 100 && $total < 200:
        $discount = 10;
        break;
    case $total >= 200 && $total < 300:
        $discount = 20;
        break;
    case $total >= 300:
        $discount = 30;
        break;
    default:
        $discount = 0;
}

echo "Discount: $discount%";

The output of this code will be:

Discount: 10%

In this example, we use multiple cases for each discount range. The switch statement evaluates each case until it finds a match, and then executes the corresponding block of code.

Example 3: Switch Statement with Fall-Through

In this example, we use a switch statement to determine the grade based on the score:

$score = 85;

switch ($score) {
    case $score >= 90:
        $grade = "A";
        break;
    case $score >= 80:
        $grade = "B";
        break;
    case $score >= 70:
        $grade = "C";
        break;
    case $score >= 60:
        $grade = "D";
        break;
    default:
        $grade = "F";
}

if ($grade == "A" || $grade == "B") {
    echo "Congratulations!";
}

echo "Your grade is $grade.";

The output of this code will be:

Congratulations! Your grade is B.

In this example, we use fall-through to execute multiple cases. The case for grade "A" also includes the code for grade "B", so if the score is between 80 and 89, both blocks of code will be executed.

Conclusion

The switch statement is a powerful tool for handling multiple conditions in PHP. It provides a concise and readable way to execute different blocks of code based on the value of a variable or expression. By using multiple cases and fall-through, you can create complex logic that is easy to understand and maintain.

Reference

Activity