PHP switch Statement

PHP Switch Statement is also called Selection Statement and this is the only Unconditional statement of PHP. That means we do not specify any kind of condition in switch statement, but we can only specify an expression or variable.

PHP Switch statement has an expression and there are some cases related to it. The case that matches the expression or the declared variable, then it is printed in the output. If any case does not match the expression, then it will print the default statement in the output. You have to break after every statement, that means it will print the statement before it. If you do not break, then the first and second will print both of these statements. Do not break after default case.

When we use a lot of if conditions in any program, the program becomes very complicated. Because of this, it is very difficult to understand and read the program. To avoid this difficulty, we use another Control Statement switch.

Just as if if statement is a two-way statement, switch is a Multi-Way Statement. It works exactly like if – elseif – else. Its general structure is as follows-

Syntax

 switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    case label3:
        code to be executed if n=label3;
        break;
    ...
    default:
        code to be executed if n is different from all labels;
}

Functionality – This structure contains value1, value2 … value n values, which are called case labels. After these, it is necessary to set up a colon, along with all of these values are different.

Statement or Statement Block 1, Statement or Statement Block 2, ……… Statement or Statement Block is a group of statements. Even though there are more than one statement in the groups of these statements, there is no need for mediated column. Even if these cells are put in place, even then PHP Interpreter does not have any problems.

Example 1

<?php
$today = date("D");
switch($today){
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    case "Wednesday":
        echo "Today is Wednesday.";
        break;
    case "Thursday":
        echo "Today is Thursday.";
        break;
    case "Friday":
        echo "Today is Friday.";
        break;
    case "Saturday":
        echo "Today is Saturday.";
        break;
    case "Sunday":
        echo "Today is Sunday.";
        break;
    default:
        echo "No information available for that day.";
        break;
}
?>

Example 2

<?php
	$percent = 90;
	$percent = (integer)($percent/10);
	
	switch($percent) {
		case 10:
		case 9:
		case 8:
			ECHO ("Pass with Honors");
		break;
			
		case 7:
		case 6:
			ECHO ("First Division Pass ");
		break;
			
		case 5:
			ECHO ("Second Division Pass ");
		break;
			
		case 4:
			ECHO ("Third Division Pass");
		break;
		
		default:
			ECHO ("Fail");
	}
?>

Sometimes we have to declare Variable Declare for a Calculation in the switch Statement. When we have to declare Variable Declare in a switch Statement, then we need to use the Block of Curly Braces.

The way we can do the Nesting of Control Statement, we can also do the Nesting of Switch Control Statement.