Use switch statement to select one of many blocks of code to be executed in PHP

Description

The following code shows how to use switch statement to select one of many blocks of code to be executed.

Example


//www . ja v  a 2s  .c  om
<?php
    $favcolor="red";

    switch ($favcolor) {
      case "red":
        echo "Your favorite color is red!";
        break;
      case "blue":
        echo "Your favorite color is blue!";
        break;
      case "green":
        echo "Your favorite color is green!";
        break;
      default:
        echo "Your favorite color is neither red, blue, or green!";
    }
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition