Articles

Tutorial on PHP statement

Learn PHP switch statements Lesson 8 Tutorials PHP

8. Switch statements PHP switch training

8. Switch statements

In the previous lesson we saw how using the elseif statement could add additional conditions to the logic statement. You can also use switch statements if you have multiple possible conditions with multiple results.

PHP Code example

$a = "one";
switch($a){
case "one":
echo "Output 1";
break;

case "'two"':
echo "Output 2":
break;

case "three":
echo "Output 3":
break;

default:
echo "output was not any of the selected cases";
break;

}

In the above example the case is the result you want to check for. Similar to having an elseif statement you only need to place the value of $a as the case. Depending on what value $a returns will be the output and code that is run for that case. Default is optional but if you want to place a false result you can use default. It would be equal to else in the regular if else statement.