# 如何根据用户的经验为用户分配费率

What syntax I should use to assign rate to user, if there would be 100 of levels. For aim to avoid hundred lines of code?

``````function showRank(\$practice) {

\$ranks = array(     "boy",
"student",
"master");

\$rank = \$ranks;

if ( \$practice >= 10 and \$practice < 20) {\$rank = \$ranks;}
else if (\$practice >= 20 and \$practice < 30) {\$rank = \$ranks;}
else if (\$practice >= 30 and \$practice < 40) {\$rank = \$ranks;}

echo \$rank;
}
``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• If you have range of 10 then you can do it.like this

``````  function showRank(\$practice) {

\$ranks = array(     "boy",
"student",
"master");

\$rank = \$ranks;
foreach (\$ranks as \$key => \$value) {
\$interval = (\$key+1)*10;
if(\$practice <= \$interval)
{
\$rank = \$value;
break;
}
}

echo \$rank;
}
showRank(20);
``````
点赞 评论 复制链接分享
• If your progression is at a constant rate, do it something like this: create `\$ranks` array like you have

``````\$ranks = array("boy", "student", "master", .....);
``````

As indicated above, taking the progression of ranks to be 10 "points" each, then

``````\$rankId = floor(\$practice/10);
\$rank = \$ranks[\$rankId];
``````
点赞 评论 复制链接分享