PHP switch Statement (With Examples)

By Shahnail Khan Last updated : December 10, 2023

If you're diving into PHP programming, you've likely come across situations where you need to make decisions based on different conditions. There comes the need for switch statements. In this tutorial, we'll explore the basics of PHP switch statements and understand how they can simplify your code.

PHP Switch Statement

A switch statement is a control structure in PHP that allows you to execute different code blocks based on the value of a specified expression. It works like multiple if-else statements and can make your code more readable and efficient.

Syntax

The syntax of PHP switch statement is:

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

PHP switch Statement Example

Let's consider a simple example where we want to determine the day of the week based on a numeric input.

<?php
$dayNumber = 3;

switch ($dayNumber) {
    case 1:
        echo "Monday";
        break;
    case 2:
        echo "Tuesday";
        break;
    case 3:
        echo "Wednesday";
        break;
    // Add cases for the remaining days
    default:
        echo "Invalid day number";
}
?>

In this example, if $dayNumber is 3, the output will be "Wednesday".

PHP switch Statement with Characters

The switch statement can also be used with characters to validate a character variable with multiple character values.

Example

This example demonstrates the PHP switch statement with characters.

<?php
$grade = "B";

switch ($grade) {
    case "A":
        echo "Excellent!";
        break;
    case "B":
        echo "Good job!";
        break;
    case "C":
        echo "You passed.";
        break;
    default:
        echo "Invalid grade.";
}
?>

The output of the above example is:

Good job!

Here, the switch statement evaluates the value of the variable $grade. Depending on the value of the $grade, it will execute the corresponding case.

In this example, since $grade is 'B', it will display "Good job!".

PHP switch Statement with Strings

String variables can also be checked with multiple string values (case values) by using the switch statement.

Example

This example demonstrates the PHP switch statement with strings.

<?php
$color = "Red";

switch ($color) {
    case "Red":
        echo "Your favorite color is red.";
        break;
    case "Blue":
        echo "Your favorite color is blue.";
        break;
    default:
        echo "You have no favorite color.";
}
?>

The output of the above example is:

Your favorite color is red.

This example uses a switch statement to check the value of the $color variable. If $color is 'Red', it prints "Your favorite color is red". Otherwise, if it's 'Blue', it prints "Your favorite color is blue". If neither, it prints "You have no favorite color".

PHP switch Statement with Integers

If you have multiple case values of integers, and you want to execute a specific case block based on the integer value. In this case, PHP switch case is used.

Example

This example demonstrates a PHP switch statement with integers.

<?php

$number = 14; // You can change the value to test different numbers

switch ($number % 2) {
    case 0:
        echo "{$number} is an even number.";
        break;
    default:
        echo "{$number} is an odd number.";
}
?>

The output of the above example is:

14 is an even number.

Here,

  • The switch statement checks the remainder when the given number is divided by 2 ($number % 2).
  • If the remainder is 0, it means the number is even, and the corresponding case is executed, printing a message stating it's an even number.
  • If the remainder is not 0 (default case), it means the number is odd, and the default case is executed, printing a message stating it's an odd number.

PHP Nested switch Statement

PHP switch statement within another switch statement is called the nested switch statement. PHP allows nested switch statements.

Example

This example demonstrates a nested switch statement.

<?php
$grade = "A";
$subject = "Math";

switch ($grade) {
    case "A":
        switch ($subject) {
            case "Math":
                echo "Excellent in Math!";
                break;
            case "English":
                echo "Excellent in English!";
                break;
            default:
                echo "Excellent in another subject.";
        }
        break;
    case "B":
        echo "Good job!";
        break;
    default:
        echo "Invalid grade.";
}
?>

The output of the above example is:

Excellent in Math!

If the grade is 'A', it checks the subject. If it's 'Math', it prints "Excellent in Math!"; if it's 'English', it prints "Excellent in English!"; otherwise, it prints "Excellent in another subject. If the grade is 'B', it prints "Good job!".

In this code, since the value assigned to grade is A, it further checks the subject. The value assigned to $subject is Math, hence it prints "Excellent in Math!".

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.