·  PHP – Syntax – Statements – Conditional – switch

 

- Info:

  - switch statement uses single integer parameter whose value determines from which case to start executing code.

  - ALL statements that follow are then executed until break; or end of switch block.

  - default: case is executed if no other cases were true and it can be placed anywhere.

  - switch statement is used instead elseif statements when all conditions evaluate the same integer variable.

  - This tutorial is based on http://hr.php.net/manual/en/control-structures.switch.php.

 

- Syntax:

  switch (integer) {

     case value1:

      optional code 1

     case value2:

      optional code 2

    ...

     default:

      optional code 10

  }

 

- Example:

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

 

      $a = 5;

      switch ($a) {

        case 0:

        case 1:

          print("Value is 1. Exit from switch. \n");

          break; #continue; will have the same effect.

        case 5:

          print("Value is 5. Continue to next case or default. \n");

        default:

          print("Default value is selected if no case was true. \n");    

      }

 

    ?>