regexp in switch statement

后端 未结 3 1198
一向
一向 2020-12-03 00:56

Are regex\'s allowed in PHP switch/case statements and how to use them ?

3条回答
  •  一个人的身影
    2020-12-03 01:26

    Yes, but you should use this technique to avoid issues when the switch argument evals to false:

    switch ($name) {
      case preg_match('/John.*/', $name) ? $name : !$name:
        // do stuff
    }
    

提交回复
热议问题