Online training and video tutorials
Enter     Registration

Max Azimov
St. Petersburg

Articles [97]

User's video [259]

PHP 5 switch Statement
The switch statement is used to perform different actions based on different conditions.

The PHP switch Statement
Use the switch statement to select one of many blocks of code to be executed.
  1. switch (n) {
  2.     case label1:
  3.         code to be executed if n=label1;
  4.         break;
  5.     case label2:
  6.         code to be executed if n=label2;
  7.         break;
  8.     case label3:
  9.         code to be executed if n=label3;
  10.         break;
  11.     ...
  12.     default:
  13.         code to be executed if n is different from all labels;
  14. }
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
  1. <?php
  2. $favcolor = "red";
  3. switch ($favcolor) {
  4.     case "red":
  5.         echo "Your favorite color is red!";
  6.         break;
  7.     case "blue":
  8.         echo "Your favorite color is blue!";
  9.         break;
  10.     case "green":
  11.         echo "Your favorite color is green!";
  12.         break;
  13.     default:
  14.         echo "Your favorite color is neither red, blue, nor green!";
  15. }
  16. ?>


Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy


The site is a media provider
English USA
Australia Русский