php switch case高级用法

1、switch会一直运行代码直到找到中断,所以很容易采用fallthrough的概念并为多个case运行相同的代码

switch($type) {
      case 3:
      case 4:
            echo "This is not the number you're looking for.\n";
            $foo = 92;
            break;
       case 5:
            echo "A copy of Ringworld is on its way to you!\n";
            $foo = 34;
            break;
}

2、在case中添加条件筛选

switch($type)
{
    case ($type > 30):
          $error = "The value provided is too long.";
          $valid = false;
          break;

    case (!preg_match('/^[A-Z0-9]+$/i', $type)):
          $error = "The value must be alphanumeric.";
          $valid = false;
          break;
    default:
          $valid = true;
          break;
}

你可能感兴趣的:(php switch case高级用法)