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[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;
}
If you have range of 10 then you can do it.like this
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);
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];