pTutorial

New way to learn PHP

PHP 5 Switch

Conditional statement switch

Switch statement is also a conditional statement like if-else, the main advantage of the switch is, Switch statement reduces the complexity of if-else.


  • The switch statement is very useful while writing menu driven script.
  • Select one of multiple conditions

Switch case is used, when we have a lot of conditions are there and want to execute one block of statement where a condition is met.


Flow chart of switch case

php 5 switch

Syntax of switch

switch(expression)
{
case label:
block of statements;
break;

case label:
block of statements;
break;

default:
block of statements;
break;
}

Example of Switch case

<?php
	$var=date("D");
	switch ($var) 
	{
		case "Sun":
			echo "It is Sunday.";
			break;
		case "Mon":
			echo "It is Monday.";
			break;
		case "Tue":
			echo "It is Tuesday."; 
			break;
		case "Wed":
			echo "It is Wednesday."; 
			break;
		case "Thu":
			echo "It is Thursday."; 
			break;
		case "Fri":
			echo "It is Friday."; 
			break;
		case "Sat":
			echo "It is Saturday."; 
			break;

	}
	
	?>

In the above example, date function gives the first three letters of the day and according to the day switch case will be executed.

.

Only one block of code will be executed where condition met.

Use of break keyword

Break is used to immediately exit out of the while, for, or switch structure in which it is contained.

  • First successful match, executes all the case block if break keyword not used.
  • Break keyword is used to explicitly break the other code blocks.

Example of switch without break keyword

<?php

$m=3;
	switch ($m) 
	{
		case 1:
		print "I am in case 1";
		case 2:
		print "I am in case 2";
		case 3:
		print "I am in case 3";
		case 4:
		print "I am in case 4";
		case 5:
		print "I am in case 5";
	}
	
	?>
	

In this example the value of $m=3 so we think only the third case executes, but it's not true after the third case all the cases will be executed.

Example of break keyword in switch case

<?php

$m=2;
switch ($m) 
{
	case 1:
		print "I am in case 1";
		break;
	case 2:
		print "I am in case 2";
		break;
	case 3:
		print "I am in case 4";
		break;
	case 4:
		print "I am in case 10";
		break;
	case 5:
		print "I am in case 3";
		break; 
}
	
?>
	

As you saw the problem in above the example, break keyword is used to remove this type of problem.

So in this case only second case executes.

Case Value

You are also allowed to use char and string values in the case and switch as shown in the following program.

Example of switch

<?php

$m=40;
	switch ($m) 
	{
        case 1:
		print "I am in case 1";
        break;
		case 20:
		print "I am in case 2";
        break;
		case 'A':
		print "I am in case A";
        break;
		case 40:
		print "I am in case 40";
        break;
		case 100:
        print "I am in case 100";
        break;
	}

?>

Output

I am in case 40

It's not mandatory to put all the cases in sequence or ascending order, you can keep all the cases in any order like above shown.

You can keep also character and as well as a string in the place of a numbers.

Execute multiple line

You can keep multiple lines of code in each case block without using any pair of braces (unlike if and else).

Example of switch

<?php

$m='A';
	switch ($m) 
	{
        case 1:
		print "I am in case 1";
        echo "
Multiple line code"; break; case 20: print "I am in case 2"; echo "
Multiple line code"; break; case 'A': print "I am in case A"; echo "
Multiple line code"; break; } ?>

Output

I am in case A
Multiple line code

Why used default keyword

  • If no one condition match successfully, then defaults block of code will be executed.

Example of default keyword

<?php

$m=30;
	switch ($m) 
	{
        case 1:
		print "I am in case 1";
        break;
		case 2:
		print "I am in case 2";
        break;
		case 3:
		print "I am in case 3";
        break;
		case 4:
		print "I am in case 4";
        break;
		case 10:
		print "I am in case 10";
        break;
		default:
		print "Default case";
	}
	
	?>
	

In the above example, the condition is not matching to the any of the case so default case will be executed.

One more thing about switch case is that, if we have no default case and no one condition is matching then simply falls through the entire switch and continue with the rest of the program.


QUOTES OF THE DAY

Whenever man commits a crime,heaven finds a witness.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER