0

私のコードでは、入れ子になった if-else 条件がたくさんあります。34回くらいやってます。本当に頭がおかしくなりそうです。減らす方法を教えてください。ありがとうございました。

そしてここに私のコード:

if ($input_age > $age_min && $input_age <$age_max){
 if ($input_heigh > $heigh_min && $input_heigh < $heigh_max){
    if ($input_ds == "yes" && $ds_val=="yes" ){ 
    //some calculation
    }
    else if ($input_ds == "no" && $ds_val=="yes"){ 
    //some calculation

    }
    else if ($input_ds == "yes" && $ds_val=="no"){ 
    //some calculation

    }
    else if ($input_ds == "no" && $ds_val=="no"){ 

    //some calculation  

    }
}
else if ($input_heigh < $heigh_min || $input_heigh > $heigh_max){
    if ($input_ds == "yes" && $ds_val=="yes" ){
    //some calculation

    }
    else if ($input_ds == "no" && $ds_val=="yes" ){ 
    //some calculation

    }

    else if ($input_ds == "yes" && $ds_val=="no"){ 
    //some calculation

    }
    else if ($input_ds == "no" && $ds_val=="no" ){ 
    //some calculation

    }
}

その他の if-else 条件

4

1 に答える 1

2

そのような場合は、はるかに読みやすい switch を使用することをお勧めします。簡単な例を次に示します。

switch($input_ds.$ds_val){
   case 'yesyes':
      //some calculations
      break;
   case 'yesno':
      //some calculations
      break;
   case 'noyes':
      //some calculations
      break;
   case 'nono':
      //some calculations
      break;
}
于 2013-05-14T08:44:34.710 に答える