I’m having trouble with a for loop within a function. The loop needs to utilise the result from the previous iteration on the subsequent one.
There are three arguments for the function: years, value and percentage rate. The function needs to loop through the number of years and deduct the percentage rate from the remaining level of value. So 3 years, a value of 100 and a percentage rate of 90%, should return 100, then 90 (100 less 10%), then 81 (90 less 10%). I would like to return these to an array that I can use.
My code so far:
<?php
function forLoop($years, $value, $rate){
$return = $value * $rate;
for ($i = 0; $i < $years; $i++) {
echo $return;
}
}
forLoop(3, 100, 0.90);
?>
This gives me the right number of returns, but gives the same value for each one. I’m not sure how to ‘save’ the previous loop’s return and use it in the next iteration, nor how to return all these results to an array. I know I should know how to do this (I’ve been programming in php on and off for 2 years ), but that makes it worse! if anyone could help me I’d be very grateful. Thank you.