-5

在这里,我使用多个 if else 如何在 switch case 中转换它。

if($ridestatus == 'Y' && $pass_status == 'Y'){
    $status = '1';
}elseif($ridestatus == 'Y' && $pass_status == 'RNS'){
    $status = 'RNS';
}elseif($ridestatus == 'RNS' && $pass_status == 'Y'){
    $status = 'RNS';
}elseif($ridestatus == 'RNS' && $pass_status == 'RNS'){
    $status = 'RNS';
}elseif($ridestatus == 'Y' && $pass_status == 'NR'){
    $status = '1';
}elseif($ridestatus == 'RNS' && $pass_status == 'NR'){
    $status = 'RNS';
}elseif($ridestatus == 'NR' && $pass_status == 'Y'){
    $status = '1';
}elseif($ridestatus == 'NR' && $pass_status == 'RNS'){
    $status = 'RNS';
}elseif($ridestatus == 'NR' || $ridestatus == '' && $pass_status == 'NR' || $pass_status == ''){
    $status = '';
}
4

2 回答 2

0

我认为没有必要将其转换为switch case您可以将其简化为,

<?php
    if($ridestatus == 'Y')
    {
        if($pass_status == 'Y' || $pass_status == 'NR')
            $status = '1';
        elseif($pass_status == 'RNS')
            $status = 'RNS';    
    }
    elseif($ridestatus == 'RNS')
    {
        if($pass_status!='')
            $status = 'RNS';
    }
    elseif($ridestatus == 'NR')
    {
        if($pass_status=='Y')
            $status = '1';
        elseif($pass_status == 'RNS')
            $status = 'RNS';
    }
    else
    {
        $status = '';
    }
?>
于 2013-07-19T08:02:22.970 回答
0

我认为你被机制束缚住了,失去了逻辑。

这应该是一个等效的测试

if ($pass_status == 'RNS' || $ridestatus == 'RNS'){
    $status = 'RNS';
} elseif ($pass_status =='Y' || $ridestatus == 'Y'){
    $status = 1;
} else {
    $status = ''; 
}
于 2013-07-19T08:06:33.687 回答