如果有100个级别,我应该使用什么语法为用户分配费率。为了避免数百行代码?
function showRank($practice) {
$ranks = array( "boy",
"student",
"master");
$rank = $ranks[0];
if ( $practice >= 10 and $practice < 20) {$rank = $ranks[1];}
else if ($practice >= 20 and $practice < 30) {$rank = $ranks[2];}
else if ($practice >= 30 and $practice < 40) {$rank = $ranks[3];}
echo $rank;
}
答案 0 :(得分:1)
如果您的进展速度恒定,请执行以下操作:
像你一样创建$ranks
数组
$ranks = array("boy", "student", "master", .....);
如上所述,将等级进展为10&#34;分数&#34;每个,然后
$rankId = floor($practice/10);
$rank = $ranks[$rankId];
答案 1 :(得分:0)
如果你有10的范围,那么你就可以做到。像这样
function showRank($practice) {
$ranks = array( "boy",
"student",
"master");
$rank = $ranks[0];
foreach ($ranks as $key => $value) {
$interval = ($key+1)*10;
if($practice <= $interval)
{
$rank = $value;
break;
}
}
echo $rank;
}
showRank(20);