我在代码中嵌套了很多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条件
我建议在这种情况下使用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;
}