PHP Introduction | Control Statements | Multiple Branches with switch

An if statement branches between two choices, true and false, but sometimes processing can branch into many choices. Of course, using elseif makes multiple branches possible with if, but long chains make the code complicated. Especially when handling numbers, you may want to process values differently, such as “if the result is 1, do this; if it is 2, do that; if it is 3, …”

The syntax used in these cases is switch. It has the following form.

switch (condition) {
case value1 :
    ... processing for value1 ...
    break;
case value2 :
    ... processing for value2 ...
    break;

    ... continue case as needed ...

default :
    ... processing when none match ...
}

You can also use a colon and endswitch instead of braces.

switch (value1):
  case value1:
    ... processing for value1 ...
    break;
  case value2:
    ... processing for value2 ...
    break;
    
  ... continue case as needed ...
  
  default:
    ... processing when none match ...
endswitch;

The switch statement also checks the condition in the parentheses and branches. However, unlike if, this condition does not have to be a logical value. It can be an ordinary number or text value.

switch checks the condition value in the parentheses and jumps to the specified case. For example, if the value there is 1, it searches for case 1: and jumps there.

What you must not forget is that switch only jumps to a case; it does not return or automatically end the necessary syntax. In other words, execution continues from top to bottom as it is. Therefore, after completing the necessary work, you must exit the statement.

The reserved word prepared for this is break. It exits the currently running statement and moves to the next operation. Remember that adding break at the end of the processing prepared for each case is the basic pattern.

If no case with the same value is found, processing moves to the final default: section. default can be omitted. In that case, if no case is found, nothing is done and execution simply continues after the statement.

Using switch, you can create very complex branches. Let’s look at a simple example.

<?php
    $b = "A"; // write the blood type
?>
<!DOCTYPE html>
<html lang="ko">
    <head> 
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" /> 
        <title>sample page</title>
    </head>
    <body>
        <h1>Hello PHP!</h1>
        <div>
            <?php
                switch($b){
                case "A":
                    echo "You are meticulous.";
                    break;
                case "B":
                    echo "You are free-spirited.";
                    break;
                case "AB":
                    echo "You are a complicated person.";
                    break;
                case "O":
                    echo "You have a big presence.";
                    break;
                default:
                    echo "You are not human.";
                }
            ?>
        </div>
    </body>
</html>

This example changes the displayed message according to the blood type. Enter a blood type in the first variable, $b. A message for that blood type is displayed.

Here, the branch is prepared with switch($b). Then each blood type is prepared as a text case, such as case "A":, and echo is executed there. If you tried to write this kind of many-way branch with if, you would need many if and elseif statements. With switch, it can be written much more cleanly.

When a case Statement Has No break;

Let’s look at one more example.

<?php
    $var = 2; 
?>
<!DOCTYPE html>
<html lang="ko">
    <head> 
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" /> 
        <title>sample page</title>
    </head>
    <body>
        <h1>Hello PHP!</h1>
        <div>
           <?php
                $var = 2;
                switch ($var) {
                    case 1:
                      echo "A";
                      break;
                    case 2:
                      echo "B";
                    case 3:
                      echo "C";
                      break;
                    default:
                      echo "Z";
                 }
             ?>
        </div>
    </body>
</html>

In this example, because the value of $var is 2, B and C are displayed. This happens because there is no break; statement inside the case 2: processing, so after displaying B, execution continues into case 3: and displays C as well.