PHP Switch statement


In this lesson, you will learn how to use the PHP switch statement which lets you an easy alternative of writing multiple if-else statements.

PHP switch Statement

PHP switch statement compares a variable or an expression against many different values and executes an associated code block if the comparison results as Boolean value true.

Example I of switch Statement in PHP

The code below contains an If-else statement

<?php 
$i = 1;
if ($i == 1) {
    echo "case 1 code block";
} elseif ($i == 2) {
    echo "case 2 code block";
} elseif ($i == 2) {
    echo "case 3 code block";
}else echo "default case";
?>

Now we convert that code to use the switch statement:

<?php
$i = 4;
 
switch ($i) {
    case 1:
        echo "case 1 code block";
        break;
    case 2:
        echo "case 2 code block";
        break;
    case 3:
        echo "case 3 code block";
        break;
    default:
        echo "default case";
}
?>

 

switch statement Flowchart

It’s quite essential to understand who the compiler parses the switch statement to avoid error in the logic. In PHP, the switch statement gets executed from the beginning to the end, one line at a time. In the beginning, no code is executed. The variable or expression given in the parenthesis of the switch is evaluated first and compare with each case presented. The code inside the case statement is executed only when the expression of that case is evaluated to be true.

PHP will continue to execute the statements of the case it matched and will exit once it encounters a break statement or when the end of the statement is reached. If you skip the break statement at the end of a case’s statement list, PHP will go on parsing the statements till the end of the switch statement. For example:

Example II of switch Statement in PHP

<?php
switch ($i) {
    case 1:
        echo "i equals 1";
    case 2:
        echo "i equals 2";
    case 3:
        echo "i equals 3";
}
?>

Here, if $i is equal to 1, PHP will execute all of the echo statements. If $i is equal to 2, PHP will execute the last two echo statements.

In PHP, the condition given in a switch statement is evaluated only once, and the result is compared to each case statement. In an else-if statement, the condition is re-evaluated.

Tip: Switch statement is faster than multiple if-else statements. So if you are writing a situation where exact values are supposed to be compared, use a switch statement instead of multiple if-else statements.

The code block for the case could be empty too, which passes control to the next case. For example:

Example III of switch Statement in PHP

<?php
switch ($i) {
    case 1:
    case 2:
    case 3:
        echo "code block 3";
        break;
}
?>

Using functions in the switch statement

In PHP you can also use functions in a switch statement.

Example of calling a function in switch Statement

<?php 
switch (true) { 
  case Function1(arg[]): 
    echo 'true'; 
  break; 
  case Fucntion2(arg[]): 
    echo 'false';
  break; 
  default: 
    echo 'default'; 
  break; 
} 
?>
PHP If-Else Tutorial Home PHP for Loop
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →