如何在SWITCH情况下转换多个if if

时间:2013-07-19 07:50:57

标签: php

这里我使用多个if 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 = '';
}

2 个答案:

答案 0 :(得分: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 = '';
    }
?>

答案 1 :(得分:0)

我认为你在机制方面受到束缚并且失去了逻辑。

这应该是一个等效的测试

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