PHP – Switch Statement


In this article, you will learn about PHP switch statement, its use to test different expressions, and know when it is used in the program instead of if-else.

PHP switch statement

php switch statement
php switch statement: default case

php switch statement

PHP switch case allows us to make a decision from numerous choices or expressions. It tests one variable against multiple conditions and executes the block whose condition is met.

In the previous lesson, we covered the various elements that make up an If Statement in PHP. However, there are times when a switch statement is more convenient so if statement is not the most efficient way to check for certain conditions.

PHP switch statement is used to avoid long blocks of if..elseif..else code.

Structure of PHP switch case

switch(expression)
 {
 case exp1: //note that instead of semi-colon a colon is used
   code block1;
   break;
 case exp2:
   code block2;
   break;
   .......
 default:
  default code block;
}


How PHP switch statement works?

PHP switch statement tests the value of an expression against a list of case values successively. For example, in the above structure, the expression is compared with all the case values. When exp1 matches the expression code block 1 will be executed and the break statement will cause an exit from the switch statement and no further comparison will be done.

However, if exp1 doesn’t matches with expression then exp2 will be compared and comparisons will be continued until the matching case. If no case matches with the expression the default code block will be executed.

Flowchart of PHP switch statement

PHP switch statement flowchart

PHP Switch Statement Example

$destination = "Tokyo";
echo "Traveling to $destination<br />";

switch ($destination){

case "Las Vegas":
  echo "Bring an extra $500";
  break;

case "Amsterdam":
  echo "Bring an open mind";
  break;

case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break;

case "Tokyo":
  echo "Bring lots of money";
  break;

case "Caribbean Islands":
  echo "Bring a swimsuit";
  break;

}

Output:

Traveling to Tokyo
Bring lots of money

Description:

The value of $destination was Tokyo so it is simply printing case “Tokyo”. So when the Switch case is activated then it looks for variables and searches for the right case.

The form of the switch statement is rather unique, so spend some time reviewing it before moving on. Note: Beginning programmers should always include the break; to avoid any unnecessary confusion.

php switch statement: default case

You may have noticed the lack of a place for code when the variable doesn’t match our condition. Same as else condition of if-else, PHP switch statement has a default case.

Example:


$destination = "New York";

 echo "Traveling to $destination<br />";

switch ($destination){

 case "Las Vegas":
  echo "Bring an extra $500";
  break;

case "Amsterdam":
  echo "Bring an open mind";
  break;

case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break;

case "Tokyo":
  echo "Bring lots of money";
  break;

case "Caribbean Islands":
  echo "Bring a swimsuit";
  break;

default:
  echo "Bring your photographer";
  break;

}

Output:

Traveling to New York
Bring your photographer