regexp in switch statement

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

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

3条回答
  •  执念已碎
    2020-12-03 01:22

    No or only limited. You could for example switch for true:

    switch (true) {
        case $a == 'A':
            break;
        case preg_match('~~', $a);
            break;
    }
    

    This basically gives you an if-elseif-else statement, but with syntax and might of switch (for example fall-through.)

提交回复
热议问题