快速浏览以下PHP开关说明了我的问题,这有点微不足道......
switch($Rank)
{
case 35:
case 37:
case 43:
case 45:
case 47:
case 53:
case 55:
case 57:
case 67:
case 65:
$MyOrd = substr($Order, 0, 3);
break;
default:
$MyOrd = '';
break;
}
switch($Rank)
{
case 45:
case 47:
case 53:
case 55:
case 57:
case 67:
case 65:
$MyFam = substr($Family, 0, 3);
break;
default:
$MyFam = '';
break;
}
我只想浓缩我的代码。有没有办法修改第一个开关,以便,而不是列出所有这些数字,从35到65,我可以简单地说“给$ MyOrd这个值,如果$ Rank = 35,65或其间的任何数值?所以新的开关可能看起来像这样:
switch($Rank)
{
case 35-65:
$MyOrd = substr($Order, 0, 3);
break;
default:
$MyOrd = '';
break;
}
答案 0 :(得分:2)
不只是用switch()
取代if()
吗?像:
if($Rank>34 && $Rank<66){
$MyOrd = substr($Order, 0, 3);
}else{
//do whatever you would do in default section
}