将php多个单词切换为一个案例

时间:2018-04-04 18:08:35

标签: php

<?php

$rank = $_POST['person'];
$name = $_POST['fname'];

switch ($rank){
    case 'MR':
        echo "Hi Mr ".$name." Wellcome To My Website";
        break;
    case 'dr':
        echo "Hi Dr ".$name." Wellcome To My Website";
        break;
    case 'miss':
            echo "Hi Miss ".$name." Wellcome To My Website";
            break;  
    case 'Prof':
            echo "Hi Prof ".$name." Wellcome To My Website";
            break;
    defualt:
        echo "Wrong";   .

我想在一个案例中添加多个关键字。像这样一些人可以进入先生或MR或先生PHP是区分大小写的语言。我想把所有先生,先生,先生的话添加到一个案例我怎么能这样做。

1 个答案:

答案 0 :(得分:2)

试试这个:

switch ($rank){
  case 'MR':
  case 'mr':
  case 'Mr':
    echo "Hi Mr ".$name." Wellcome To My Website";
    break;
  case 'dr':
    echo "Hi Dr ".$name." Wellcome To My Website";
    break;
  case 'miss':
        echo "Hi Miss ".$name." Wellcome To My Website";
        break;  
  case 'Prof':
        echo "Hi Prof ".$name." Wellcome To My Website";
        break;
  default:
    echo "Wrong";

对于一个事件的多个案例,这将做你想要的。但是,我建议您将switch语句更改为以下内容:

switch (strtolower($rank)){

然后只检查小写值。