PHP

PHP Switch Case Statement

PHP Switch Case Statement
Written by ProgrammingPot

In this tutorial, we will learn about the PHP Switch Case Statement to test or evaluate an expression with different values in PHP.

PHP Switch Case Statement


The PHP Switch Case Statement is an alternative to the if elseif else statement, which does almost the same thing. Switch Case acts similar to Elseif control structure.

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

The following illustrates the PHP Switch Case Statement syntax:


<?php
switch(variable){
 case value1: 
 // code block 1
 break;
 case value2: 
 // code block 2
 break;
 
 default:     
 // default code block
 break;
}

Let’s examine the switch statement syntax in more detail:

  • First, you put a variable or expression that you want to test within parentheses after the switch keyword.
  • Second, inside the curly braces are multiple case constructs where you put the values you want to compare with the variable or expression. In case the value of the variable or expression matches the value in a case construct, the code block in the corresponding case construct will execute. If the value of the variable or expression does not match any value, the code block in the default construct will execute.
  • Third, the break statement is used in each case or default construct to exit the entire switch statement.

It is very important to understand that the switch statement is executed statement by statement therefore the order of  case constructs is very important. If the value of the variable match a value in a case construct.

PHP will execute code block in that case construct and ends the switch statement because of the break statement.

Example of PHP Switch Case Statement:


In the following example $xint is equal to 3, therefore switch statement executes the third echo statement

<?php
$xint=3;
switch($xint) {
case 1:
echo "This is case No 1.";
break;
case 2:
echo "This is case No 2.";
break;
case 3:
echo "This is case No 3.";
break;
case 4:
echo "This is case No 4.";
break;
default:
echo "This is default.";
}
?>

Output:

This is case No 3.

The PHP Switch Case Statement differs from the if-elseif-else statement in one important way. The switch statement executes line by line (i.e. statement by statement) and once PHP finds a case statement that evaluates to true.

it’s not only executes the code corresponding to that case statement, but also executes all the subsequent case statements till the end of the switch block automatically.

To prevent this add a break statement to the end of each case block. The break statement tells PHP to break out of the switch-case statement block once it executes the code associated with the first true case.


Leave a Comment