post

Learn the PHP Switch Statement

The PHP switch statement is a conditional statement used to select one of many possible blocks of code, which are called cases. By comparing the value of the expression to the value of each defined case, the switch statement will choose the correct block of code to execute. Unlike if statements, switches work by evaluating a single expression or variable as input for multiple possible values at the same time. Using a switch can be a much more efficient way to evaluate a single value as opposed to writing several large if/elseif/else statements.
 
 

PHP Switch Statement Cases

Your switch statement’s cases must be enclosed in braces characters { } and each case must be proceeded by a colon :. Double quotes “ “ around the value of the case do not need to be used every time, however, it may be easier to include them.
 

Example PHP Switch Statement

<?php
$expression=1;

switch ($expression)
{ //Start switch statement cases
    case "1": //Case chosen if the value for the expression is 1.
        echo "Code executed if the expression evaluates to 1. <br />";

    case "2": //Case chosen if the value for the expression is 2.
        echo "Code executed if the expression evaluates to 2. <br />";

} //End switch statement cases
?>

 

Result:

Code executed if the expression evaluates to 1.
Code executed if the expression evaluates to 2.

 
 

Exiting a PHP Switch Statement

 
In the example above, you probably noticed that the code for cases one and two were executed even though the expression’s value was 1. That is because each case should include break; after the lines of code that you want executed when a case is selected. The break; tells your script that it is time to stop executing code within your switch statement and to move on to the next part of the script. If you fail to include break; your code will continue executing even after it encounters the next instance of case.
 

Exiting a PHP Switch Statement Example

<?php
$expression=1;

switch ($expression)
{ 
    case "1":
        echo "The expression evaluates to 1 and executes only the code in this case. <br />";
        break; //Exit the switch statement.
    case "2":
        echo "Code executed if the expression evaluates to 2. <br />";
        break; //Exit the switch statement.
} 
?>

 

Result:

The expression evaluates to 1 and executes only the code in this case.

 
 

Switch Statement Default Code

 
Like else in an if statement, PHP switch can include a default: block of code that will be executed when no defined cases match the expression value that you are evaluating for.
 

PHP Switch Default Code Block

<?php
$expression="Hello World!";

switch ($expression)
{ 
    case "1":
        echo "Code executed if the expression evaluates to 1. <br />";
        break;
    case "2":
        echo "Code executed if the expression evaluates to 2. <br />";
        break;
    default: //Case chosen if no other case matches the expression's value.
        echo "The value of the expression does not match any defined case. <br />";
        break;
}
?>

 

Result:

The value of the expression does not match any defined case.

 

Using PHP Switch in a Practical Application

Let’s test the PHP switch statement by writing a simple script to echo a different message for every day of the week.

<?php
switch (Date ("D")) //Date ("D") retrieves the first three letters of the current day.
{ 
case "Sun":
    echo "It is Sunday. ";
    echo "Don't waste time in church, learn PHP instead. <br />";
    break;
case "Mon":
    echo "It is Monday. ";    
    echo "I am sorry. <br />";
    break;
case "Tue":
    echo "It is Tuesday. ";    
    echo "Don't worry, you will survive. <br />";
    break;
case "Wed":
    echo "It is Wednesday. ";
    echo "The work week is half over! <br />";
    break;
case "Thu":
    echo "It is Thursday. ";
    echo "I have to work late today. I hope you have a better day than me. <br />";
    break;
case "Fri":
    echo "It is Friday. ";
    echo "Have a great weekend! <br />";
    break;
case "Sat":
    echo "It is Saturday. ";        
    echo "Don't drink and drive. <br />";
    break;
default:
    echo "Default will never be called because all possibilities have been accounted for. <br />";
    break;
}
?>

 

Result:

It is Saturday. Don't drink and drive.

 

Speak Your Mind

*

*