Saturday, May 3, 2014

PHP switch Statement

The PHP switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax

switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
  ...
  default:
    code to be executed if n is different from all labels;
Example:
<?php
$number = -10

switch ($number) {
  case 1:
     echo "Your number is one!";
    break;
  case 2:
     echo "Your number is two!";
    break;
  case 3:
    echo "Your number is three!";
    break;
  default:
    echo "Number not Found!";
}
?> 

Output: Number not Found!

No comments:

Post a Comment