I’m trying to create a levelling and experience system. I’ve done it but I’m trying to get PHP to work out how much experience is needed to level up.
For every level, an user needs 670 experience. But if I added 670 to their experience total, it wouldn’t work it out correctly.
This is how I did it..
<?php $exp = ($user["experience"]+670); ?>
But that isn’t working how much the user needs. Hope I’ve explained clearly. Any help will be nice, thanks.
Think about something along these lines:
The expression
$user["experience"] % 670would give you the amount over from the last level. So if I had 671 experience, it would give 1, if I had 1360 experience, it would give 20, etc. Then you can subtract this amount from 670 to give the amount left.(Also, as a general tip, it’s helpful when coding if you have a so-called ‘magic number’ like 670, you could give it a name by defining a constant called e.g. EXP_PER_LEVEL, rather than using the bare value 670, so that if anyone’s reading your code in future, they can easily work out what the 670 means, and also, if the amount of exp per level ever changes, you only have to change the value in one place, i.e. the place where the EXP_PER_LEVEL constant is defined)