slyte33 Posted September 9, 2010 Share Posted September 9, 2010 I am working on my MMORPG. I am looking to get the difference in EXP per level. Take a look at this code below: for($i=1;$i<=10;$i++) { $xp2+=10*$i; $xp=(1.3726*$i)*$xp2; echo "$i. ".number_format($xp).""; echo "<br>"; if ($i!=1) { $diff= //FORMULA TO FIND THE DIFFERENCE echo "[DIFF: ".number_format($diff)." XP]"; } echo "<br><br>"; } The above would output: 1. 14 2. 82 [DIFF: 0 XP] 3. 247 [DIFF: 0 XP] 4. 549 [DIFF: 0 XP] 5. 1,029 [DIFF: 0 XP] 6. 1,729 [DIFF: 0 XP] 7. 2,690 [DIFF: 0 XP] 8. 3,953 [DIFF: 0 XP] 9. 5,559 [DIFF: 0 XP] 10. 7,549 [DIFF: 0 XP] As you can see, 2. 82-14=68 3. 247-82=165 and so on. I would just like the difference. I spent too much time trying to get this right, thanks Quote Link to comment https://forums.phpfreaks.com/topic/212973-fairly-simply-math-formula-getting-difference-in-numbers/ Share on other sites More sharing options...
Daniel0 Posted September 9, 2010 Share Posted September 9, 2010 Just store the previous one and subtract them: <?php $prev = null; $xp2 = 0; for($i=1;$i<=10;$i++) { $xp2+=10*$i; $xp=(1.3726*$i)*$xp2; echo "$i. ".number_format($xp).""; echo "<br>"; if ($i!=1) { $diff = $xp - $prev; echo "[DIFF: ".number_format($diff)." XP]"; } echo "<br><br>"; $prev = $xp; } Quote Link to comment https://forums.phpfreaks.com/topic/212973-fairly-simply-math-formula-getting-difference-in-numbers/#findComment-1109260 Share on other sites More sharing options...
slyte33 Posted September 9, 2010 Author Share Posted September 9, 2010 Just store the previous one and subtract them: <?php $prev = null; $xp2 = 0; for($i=1;$i<=10;$i++) { $xp2+=10*$i; $xp=(1.3726*$i)*$xp2; echo "$i. ".number_format($xp).""; echo "<br>"; if ($i!=1) { $diff = $xp - $prev; echo "[DIFF: ".number_format($diff)." XP]"; } echo "<br><br>"; $prev = $xp; } Ahh so simple and effective. Learn something new about PHP everytime I come here whether I ask a question or not, thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/212973-fairly-simply-math-formula-getting-difference-in-numbers/#findComment-1109264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.