Actions

 Language:
 RSS flow:


switch statement


Overview

The switch statement is equivalent to a set of "if" instructions. It allows you to run different parts of code depending on the value to which it is equal.

Codes Example (with numerical values):

<?php
 switch ($i) {
  case 0:
    echo "i is equal to 0";
    break;
  case 1:
    echo "i is equal to 1";
    break;
  case 2:
    echo "i is equal to 2";
    break;
  default:
    echo "i is outside the limits";
 }
?>

Codes Examples (with strings):

<?php
 switch ($i) {
  case "OK":
    echo "The test is OK.";
    break;
  case "incompatible":
    echo "The solution is incompatible.";
    break;
  case "KO":
    echo "Incorrect test.";
    break;
 }
?>
Go back