Newest Visitor Badge Earned!
Click to create an account and start competing today!
After Hours Programming
Show Tutorials


You (Level 0)
0/25
Last Badge Earned
Newest Visitor

PHP Switch Statement Tutorial

PHP Switch Statement

The PHP Switch Statement tutorial explains how to use a super if statement, a switch statement.

The PHP switch statement is pretty much a simplified way to write multiple if statements for one variable. As you use them, you will begin to realize why they are much more convenient that writing a whole lot of if statements or elseif statements. If statements check one conditional, but switch statements can check for many different cases. The simple syntax of switch statements provide more readable code as opposed to using a lot of else if statements.

Example $x = 3;
switch($x)
{
case 1: //this statement is the same as if($x == 1)
echo 'Case 1 was executed.';
break;
case 2: //this statement is the same as if($x == 2)
echo 'Case 2 was executed.';
break;
case 3: //this statement is the same as if($x == 3)
echo 'Case 3 was executed.';
break;
case 4: //this statement is the same as if($x == 4)
echo 'Case 4 was executed.';
break;
default: //this statement is the same as if $x does not equal the other conditions
echo 'Default was executed.';
break;
}
Result Case 3 was executed.

The syntax is slightly different than an if statement. The entire switch is implicitly using the == that we saw in the if statements earlier. However, we can see that we do not have to repeat that boring comparison operator over and over. Instead, the case is followed by the conditional variable. After the case 1, we see a :. After that colon, we have our statements to be executed. Finally, we come to the break, which signals the end of the if like statement. If we didn't use break, PHP would continue to execute the other conditions in the switch statement. So, use break at the end of your case block to break out of the switch statement unless you want the following cases to be executed. As for the default:, it means that if none of the other conditions are satisfied, do the statements following the default:. The default term is comparable to the else statement.

Try testing your code with the code simulator!

Let's move on to some more tutorials or the next section!



Previous Tutorial

If Statements

Next Tutorial

Functions







If you enjoyed this resource, support me by sharing this page with others.

Stumbleupon Facebook Twitter GooglePlus Reddit Delicious Digg


Comment or Suggestion?

Comments are a way to tell me about corrections or suggestions, what you thought about the resource, or providing additional information. Help feed the discussion!


Yes




Comments on PHP Switch Statement

user
Mohsen Qaddoura
February 9, 2014 09:05AM
When trying the example code ( by copying and pasting in the provided code simulator error messages are indicated for two reasons:

1-First: using the  word " executed" in the echo statement string is mistaken for an executive file!
2- Second : code doesn't execute unless parentheses is provide to enclose the variable value.


user
Donna
100
July 16, 2013 10:12PM
You neglected to mention the need for the break statement. without a break statement, 'each' of the lines will execute. 
E.G.
In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

<?php
switch ($i) {
    case 0:
        echo "i equals 0";
    case 1:
        echo "i equals 1";
    case 2:
        echo "i equals 2";
}
?>
Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.

The statement list for a case can also be empty, which simply passes control into the statement list for the next case.

<?php
switch ($i) {
case 0:
case 1:
case 2:
    echo "i is less than 3 but not negative";
    break;
case 3:
    echo "i is 3";
}
?>

ref: php manual: http://www.php.net/manual/en/control-structures.switch.php