# Calculate Player Level in PHP

So in my program, I currently know how much Experience a player has. I was using this formula to figure out what level the Player was:

function calcLevel($XP,$const){
$L = sqrt($XP)*$const; return floor($L+1);
}


Then I wanted to figure out what Percentage of the way the player was to the next level, so I needed to figure out how much Experience they needed to get to that point. I thought this would work:

function xpNeeded($L,$const){
$XP = pow($L,2)/$const; return$XP;
}


I would also need to know how much previous Experience the player had earned in other levels so I could calculate the percentage based on the Experience earned at the current level only:

function prevXP($L,$const){
$XP = 0; for($i=1;$i<$L;$i++){$XP = $XP + xpNeeded($i,$const); } return$XP;
}


This all made sense to me, but the calculations are a bit off.

For instance, using Level 2, and Constant of .1:

$const = .1;$lvl = 2;
$a = xpNeeded($lvl,$const); //returns 40$b = prevXP($lvl,$const); //returns 10
$c =$a+$b; //50 calcLevel($c,$const) //returns 1, should return 2  I can't really tell where my calculations have gone wrong, obviously, it is in the xpNeeded function, I just do not know what I did wrong. ## 1 Answer Disclaimer: I went on a tangent from xpNeeded and prevXP because they were confusing me and they were not working anyway. Instead I focused on how to go from the function to compute the level to know how much progress the player is doing to the next level. ## Basic functions Starting by the function you have for the level: function calcLevel($XP, $const){$L = sqrt($XP)*$const;
return floor($L+1); }  Note: I assume that 1 is there because we count level from 1 and not from 0. That is, when a player has 0 XP, that is level 1 instead of level 0. I will write it like this: function calcLevel($XP, $const){$L = floor(1 + sqrt($XP)*$const);
return $L; }  We should be able to figure out a formula that given the level gives you the XP back. It should be the inverse formula... So, we have that L = [1 + sqrt(XP)*c]  I will ignore the floor function. You can add a floor call to ensure the level is integer. So... L = 1 + sqrt(XP)*c => L - 1 = sqrt(XP)*c => (L - 1) / c = sqrt(XP) => ((L - 1) / c)^2 = XP  Thus function calcXP($L, $const){$XPsqrt = ($L - 1) /$const;
$XP =$XPsqrt * $XPsqrt; return$XP;
}


Note that gives you the XP to get from 0 XP to the level you input.

## Experience to the next level

If you want to know how much the player needs to get to the next level, having the xp as input, you do this:

function xpToNextLevelFromXP($XP,$const){
$L = calcLevel($XP, $const); return calcXP($L + 1, $const) -$XP;
}


What we are doing here is computing how much XP the player needs to get from 0 XP to the next level and substract the XP the player already has.

If you want to inline and simplfy the function, it looks like this:

function xpToNextLevelFromXP($XP,$const){
$L = floor(1 + sqrt($XP)*$const);$XPsqrt = $L /$const;
return ($XPsqrt *$XPsqrt) - $XP; }  However, if you have the level instead of the XP, you do this instead: function xpToNextLevelFromLevel($L, $const){ return calcXP($L + 1, $const) - calcXP($L, $const); }  Or inlined: function xpToNextLevelFromLevel($L, $const){$XPNsqrt = ($L) /$const;
$XPsqrt = ($L - 1) / $const; return ($XPNsqrt * $XPNsqrt) - ($XPsqrt * $XPsqrt); }  ## Progress to the next level If you want to know how much XP the player has in the current level you can do this: function xpInLevel($XP, $const){$L = calcLevel($XP,$const);
return $XP - calcXP($L, $const); }  That is, we substract from the XP the player has, how much XP is needed to get to the current level. Of course, you can work out the percentage progress to the next level from the current one using the functions above: function progressLevel($XP, $const){$total = xpToNextLevel($XP,$const);
$current = xpInLevel($XP, $const); return$current / $total; }  The above function should return a value between 0 and 1 which represent the progress. Multiply by 100 for display as percentage. We can, inline the functions, and remove the redundant call to calcLevel: function progressLevel($XP, $const){$L = calcLevel($XP,$const);
$total = calcXP($L + 1, $const) -$XP;
$current =$XP - calcXP($L,$const);
return $current /$total;
}


To be honest. As I said at the start, xpNeeded and prevXP confuse me. Futhermore, I do not understand your example. Anyway, here is an approximation:

function calcXP($L,$const){
$XPsqrt = ($L - 1) / $const;$XP = $XPsqrt *$XPsqrt;
return $XP; } function calcLevel($XP,$const){$L = floor(1 + sqrt($XP)*$const);
return $L; } function xpToNextLevelFromLevel($L, $const){$XPNsqrt = ($L) /$const;
$XPsqrt = ($L - 1) / $const; return ($XPNsqrt * $XPNsqrt) - ($XPsqrt * $XPsqrt); }$const = .1;
$lvl = 2;$a = xpToNextLevelFromLevel($lvl,$const); // 300
$b = calcXP($lvl,$const); // 100$c = $a+$b; // 400
$r = calcLevel($c,\$const); // 3


This tell us that:

• From level 2 to level 3 you need 300 XP.
• To get to level 2 (from level 1, which is the starting point, a.k.a 0 XP) you need 100 XP.
• If you have 300 XP + 100 XP = 400 XP, you are in level 3 (which is the next level after level 2).