php switch case multiple values code example

Example 1: php switch

$age=20;
switch ($age) {
	case 19:
        echo "Age is 18";
        break;
    case 20:
        echo "Age is 20";
        break;
    case 21:
        echo "Age is 21";
        break;
}

Example 2: switch case php

$fruit = "apple";
switch ($fruit) {
  case "apple":
    echo "doctor";
    break;
  case "banana":
    echo "monkey";
    break;
  default:
   	echo "things";
}//doctor

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 switch 2 variables

switch ([$color, $size]) {
    case ['blue', 'small']:
        echo 'blue and small';
    break;

    case ['red', 'large'];
        echo 'red and large';
    break;
}

Example 5: dualcase php switch

case 'car':
case 'ferrari':
    print("car or ferrari");
    break;

Tags:

Php Example