PHP Switch Case Statement

Switch statement is useful where we have to select one option from 'n' number of choices. Keyword switch is use to define switch statement with variable to check with the case value, keyword case is used inside switch statement with value. Value can be integer or character only it cannot be float data type.

PHP Switch Case

Example 1: Switch Case - Display day.

<?php
    echo "<h2>Find Day.</h2>";
    $a = 3;
    switch($a)
    {
        case 1:
            echo "Today is Monday";
        break;
        case 2:
            echo "Today is Tuesday";
        break;
        case 3:
            echo "Today is Wednesday";
        break;
        case 4:
            echo "Today is Thursday";
        break;
        case 5:
            echo "Today is Friday";
        break;
        case 6:
            echo "Today is Saturday";
        break;
        case 7:
            echo "Today is Sunday";
        break;
        default:
            echo "Invalid Number";
        break;
    }
?>
Today is Wednesday

Example 2: Switch Case - get current day and echo a message

<html>
    <body>
    <?php
        $d=date("D"); //get current day
        switch ($d)
        {
            case "Mon":
                echo "Today is Monday";
            break;
            case "Tue":
                echo "Today is Tuesday";
            break;
            case "Wed":
                echo "Today is Wednesday";
            break;
            case "Thu":
                echo "Today is Thursday";
            break;
            case "Fri":
                echo "Today is Friday";
            break;
            case "Sat":
                echo "Today is Saturday";
            break;
            case "Sun":
                echo "Today is Sunday";
            break;
            default:
                echo "Wonder which day is this ?";
       }
    ?>
  </body>
</html>
It will fetch currect day and print message according to the case matched.

Example 3: Switch Case - Input a number between 1 to 7 and display cooresponding day.

<html>
<body>
	<h2>Input Number (1-7) and Find Day</h2>
	<form action="" method="get">
	    <p>Enter Number: <input type="number" name="n"></p>
	    <button type="submit">Find Day</button>
	</form>
<?php
	$a = $_GET['n'];
	switch($a)
	{
        case 1:
            echo "Today is Monday";
        break;
        case 2:
            echo "Today is Tuesday";
        break;
        case 3:
            echo "Today is Wednesday";
        break;
        case 4:
            echo "Today is Thursday";
        break;
        case 5:
            echo "Today is Friday";
        break;
        case 6:
            echo "Today is Saturday";
        break;
        case 7:
            echo "Today is Sunday";
        break;
        default:
            echo "Invalid Number";
        break;
    }
?>
</body>
</html>

Example 4: Switch with multiple case/choices - Input a number between 1 to 31 and display cooresponding day.

<html>
<body>
	<h2>Input Today Date (1 - 31) and Find Day</h2>
	<form action="" method="get">
		<p>Enter Number: <input type="number" name="n" placeholder="dd"></p>
		<button type="submit">Find Day</button>
	</form>
<?php
	$a = $_GET['n'];
	switch($a)
	{
		case 1:
		case 8:
		case 15:
		case 22:
		case 29:
			echo "Today is Monday";
		break;
		case 2:
		case 9:
		case 16:
		case 23:
		case 30:
			echo "Today is Tuesday";
		break;
		case 3:
		case 10:
		case 17:
		case 24:
		case 31:
			echo "Today is Wednesday";
		break;
		case 4:
		case 11:
		case 18:
		case 25:
			echo "Today is Thursday";
		break;
		case 5:
		case 12:
		case 19:
		case 26:
			echo "Today is Friday";
		break;
		case 6:
		case 13:
		case 20:
		case 27:
			echo "Today is Saturday";
		break;
		case 7:
		case 14:
		case 21:
		case 28:
			echo "Today is Sunday";
		break;
		default:
			echo "Invalid Number";
		break;
	}
?>
</body>
</html>

Exercise Questions: Switch case

  1. Input character and find it is vowel or not.
  2. <?php
        $alpha="u";
        switch($alpha)
        {
            case "a":
            case "e":
            case "i":
            case "o":
            case "u":
                echo "Character is a vowel";
            break;
            default:
                echo "Character is not a vowel";
            break;
        }
    >?