Let’s say I have this code:
<?php
$aLevel[] = 98;
function experience($L) {
$a=0;
for($x=1; $x<$L; $x++) {
$a += floor($x+300*pow(2, ($x/7)));
$aLevel[$x-1] = $a; // we minus one to comply with array
}
return floor($a/4);
}
for($L=1;$L<100;$L++) {
echo 'Level '.$L.': '. number_format(experience($L)). '<br />';
}
echo $aLevel[0]; // Level 1 should output 0 exp
echo "<br />" . $aLevel[1]; // Level 2 should output 83 exp
// et cetera
?>
I am trying to make an array to store the exp. So level 1 would be $aLevel[0] and the EXP would be 0 (obviously) and level 2 would be $aLevel[1] and EXP would be 83 and so on.
The code below… it works. The experience and level loop works but the array doesn’t.
What am I doing wrong?
Aside from your scoping issue (the
$aLevelused inside the function is not the same as outside), you are calculating the experience WAY too many times. When $L = 98, you calculate experience for levels 1-97 and then when $L = 99 you do them all over again. Also, you’re dividing your return value by 4 but not the values you’re storing in the array.Assuming I understand the algorithm you’re going for, this is how I might do it: