Switch case In PHP

The Switch case In PHP is used to perform one of several different actions based on one of several different conditions. SWITCH is used in PHP to replace nested IF..ELSE loops, and is similar to the CASE command in other computer languages.
With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation!
The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.
switch (expression)
case label1:
  code to be executed if expression = label1;
case label2:
  code to be executed if expression = label2;
  code to be executed
  if expression is different 
  from both label1 and label2;

	case 1:
			echo "<today is <b>Monday</b>";
	case 2:
			echo "Today is <b>Tuesday</b>";
		echo "<b>Invalid Day Number ".$day."</b>";

Type the above code and save it as switch-case.php and open the page in browser to view the output.
Scroll to Top