switch case in php with multiple case example

Example 1: case statement in php

<?php
switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}
?>

Example 2: php switich

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 3: php switch case multiple values per line

<?php
switch ($i) {
    case 0:
    case 1:
    case 2:
        echo "i is less than 3 but not negative";
        break;
    case 3:
        echo "i is 3";
}
?>

Example 4: php $_post switch case

<?php
  if (isset($_POST[beermoney])){$i = $_POST[beermoney]};
switch ($i) {
    case 0:
        echo "get a job";
        break;
    case 1:
        echo "buy draft beer";
        break;
    case 2:
        echo "drink budwieser";
        break;
}
?>

Tags:

Php Example