2

OK, let's say I have this small snippit of a bunch of else-ifs in PHP

function getLevelByExp($theExp) {

    if ($theExp < 83) { $theLevel = 1; }
else if ($theExp < 175) { $theLevel = 2; }
else if ($theExp < 278) { $theLevel = 3; }
else if ($theExp < 392) { $theLevel = 4; }
else if ($theExp < 519) { $theLevel = 5; }
else if ($theExp < 659) { $theLevel = 6; }
else if ($theExp < 814) { $theLevel = 7; }
else if ($theExp < 987) { $theLevel = 8; }
else if ($theExp < 1178) { $theLevel = 9; }
else if ($theExp < 1390) { $theLevel = 10; }
else if ($theExp < 1624) { $theLevel = 11; }
else if ($theExp < 1884) { $theLevel = 12; }
else if ($theExp < 2172) { $theLevel = 13; }
else if ($theExp < 2492) { $theLevel = 14; }
else if ($theExp < 2845) { $theLevel = 15; }
else if ($theExp < 3237) { $theLevel = 16; }
else if ($theExp < 3671) { $theLevel = 17; }
else if ($theExp < 4151) { $theLevel = 18; }
else if ($theExp < 4683) { $theLevel = 19; }
else if ($theExp < 5273) { $theLevel = 20; }
else if ($theExp < 5926) { $theLevel = 21; }
else if ($theExp < 6650) { $theLevel = 22; }
else if ($theExp < 7451) { $theLevel = 23; }
else if ($theExp < 8339) { $theLevel = 24; }
else if ($theExp < 9322) { $theLevel = 25; }
else if ($theExp < 10412) { $theLevel = 26; }
else if ($theExp < 11618) { $theLevel = 27; }
else if ($theExp < 12955) { $theLevel = 28; }
else if ($theExp < 14436) { $theLevel = 29; }
else if ($theExp < 16076) { $theLevel = 30; }
else if ($theExp < 17893) { $theLevel = 31; }
else if ($theExp < 19906) { $theLevel = 32; }
else if ($theExp < 22137) { $theLevel = 33; }
else if ($theExp < 24607) { $theLevel = 34; }
else if ($theExp < 27345) { $theLevel = 35; }
else if ($theExp < 30377) { $theLevel = 36; }
else if ($theExp < 33737) { $theLevel = 37; }
else if ($theExp < 37460) { $theLevel = 38; }
else if ($theExp < 41584) { $theLevel = 39; }
else if ($theExp < 46154) { $theLevel = 40; }
else if ($theExp < 51217) { $theLevel = 41; }
else if ($theExp < 56827) { $theLevel = 42; }
else if ($theExp < 63042) { $theLevel = 43; }
else if ($theExp < 69929) { $theLevel = 44; }
else if ($theExp < 77560) { $theLevel = 45; }
else if ($theExp < 86015) { $theLevel = 46; }
else if ($theExp < 95383) { $theLevel = 47; }
else if ($theExp < 105764) { $theLevel = 48; }
else if ($theExp < 117266) { $theLevel = 49; }
else if ($theExp < 130010) { $theLevel = 50; }
else if ($theExp < 144132) { $theLevel = 51; }
else if ($theExp < 159781) { $theLevel = 52; }
else if ($theExp < 177120) { $theLevel = 53; }
else if ($theExp < 196333) { $theLevel = 54; }
else if ($theExp < 217623) { $theLevel = 55; }
else if ($theExp < 241215) { $theLevel = 56; }
else if ($theExp < 267356) { $theLevel = 57; }
else if ($theExp < 296323) { $theLevel = 58; }
else if ($theExp < 328421) { $theLevel = 59; }
else if ($theExp < 363990) { $theLevel = 60; }
else if ($theExp < 403404) { $theLevel = 61; }
else if ($theExp < 447078) { $theLevel = 62; }
else if ($theExp < 495475) { $theLevel = 63; }
else if ($theExp < 549104) { $theLevel = 64; }
else if ($theExp < 608531) { $theLevel = 65; }
else if ($theExp < 674384) { $theLevel = 66; }
else if ($theExp < 747357) { $theLevel = 67; }
else if ($theExp < 828220) { $theLevel = 68; }
else if ($theExp < 917826) { $theLevel = 69; }
else if ($theExp < 1017122) { $theLevel = 70; }
else if ($theExp < 1127154) { $theLevel = 71; }
else if ($theExp < 1249084) { $theLevel = 72; }
else if ($theExp < 1384199) { $theLevel = 73; }
else if ($theExp < 1533925) { $theLevel = 74; }
else if ($theExp < 1699840) { $theLevel = 75; }
else if ($theExp < 1883698) { $theLevel = 76; }
else if ($theExp < 2087436) { $theLevel = 77; }
else if ($theExp < 2313207) { $theLevel = 78; }
else if ($theExp < 2563392) { $theLevel = 79; }
else if ($theExp < 2840632) { $theLevel = 80; }
else if ($theExp < 3147852) { $theLevel = 81; }
else if ($theExp < 3488295) { $theLevel = 82; }
else if ($theExp < 3865553) { $theLevel = 83; }
else if ($theExp < 4283609) { $theLevel = 84; }
else if ($theExp < 4746873) { $theLevel = 85; }
else if ($theExp < 5260236) { $theLevel = 86; }
else if ($theExp < 5829115) { $theLevel = 87; }
else if ($theExp < 6459514) { $theLevel = 88; }
else if ($theExp < 7158087) { $theLevel = 89; }
else if ($theExp < 7932204) { $theLevel = 90; }
else if ($theExp < 8790038) { $theLevel = 91; }
else if ($theExp < 9740641) { $theLevel = 92; }
else if ($theExp < 10794045) { $theLevel = 93; }
else if ($theExp < 11961369) { $theLevel = 94; }
else if ($theExp < 13254932) { $theLevel = 95; }
else if ($theExp < 14688387) { $theLevel = 96; }
else if ($theExp < 16276862) { $theLevel = 97; }
else if ($theExp < 18037122) { $theLevel = 98; }
else if ($theExp < 20000000) { $theLevel = 99; }
else { $theLevel = 99; }

return $theLevel;

}

As you can tell, I can easily get the Level, using the experience points, like $currentLevel = getLevelByExp(300) which would output: 3.

However, would I make it so it's reversed? Get the EXPERIENCE by Level so if I enter getExpByLevel(3) it would output: 392?

3
  • From that what You have given us the user could have experience between 278 and 392 to have the level 3. So You cannot get the EX exactly. You could only get the minimum or maximum EX needed for level 3. Commented Dec 6, 2012 at 9:50
  • Where do these values come from? How did you calculate them or are they just random numbers? If you calculated them you can reverse it to get the level for a specific experience Commented Dec 6, 2012 at 9:54
  • 1
    I recommend you to use a mathematic relation beween level and exp. Commented Dec 6, 2012 at 9:55

2 Answers 2

2

This should do the trick

// level 0: 0 exp, level 1: 83 exp, level 2: 175 exp, level 3: 278 exp, ...
$levels = array(0, 83, 175, 278 /*, ... */);

// get level for theExp
$theExp = 180;

foreach ($levels as $level => $exp) {
    if($theExp < $exp) break;
}

// output level
echo "the level for {$theExp} is {$level}";
// => 2

To get this wrapped up using some nice encapsulation

class Player

    static private $levels = array(0, 83, 175, 278);
    static private $max_level = 50;

    private $exp;

    public function __construct($exp) {
        $this->exp = $exp;
    }

    public function getLevel() {

        // find level based on exp
        foreach (self::$levels as $level => $exp) {
            if ($this->exp < $exp)
                return $level;
        }

        // no level found; return max level
        return self::$max_level;
    }
}

Here's the usage

// create new player with 180 exp
$ply = new Player(180);

// get player level
echo $ply->getLevel(); //=> 2

Thanks for the max level tip, @EugenRieck

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think I should be coding at 4AM. I'll go sleep now.........(I think you should theExp and Level around)
@weka, since arrays already serialize their numerical indexes, it makes perfect sense to have the array index represent the level.
0
$EXP_LEVEL_MAP=array (0,83,175,278,392,....20000000);

function lvlFromExp($exp) {
  global $EXP_LEVEL_MAP;
  foreach($EXP_LEVEL_MAP as $k=>$v)
    if ($v>$exp)
      return $k;
  return 99;
}

function expFromLvl($lvl) {
  global $EXP_LEVEL_MAP;
  if ($lvl>99) $lvl=99;
  else if ($lvl<1) $lvl=1M
  return $EXP_LEVEL_MAP[$lvl];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.