计算php中的复合兴趣

Compliments all, Am quite new in working with php but i have to try somehow... My problem is this, the formular for compound interest is A = P(1 + r/n) ^ nt Now, if i got $60000 to invest for 1 years at 15%, my interest gained would be $9000, and if i add it to my initial $60000 therefore the final amount = $69000. Here's the catch. if the year is more than 1yr like 2yrs, i want to add my initial ($60000) to the final amount ($69000) to get a new value = $129000 which i will multiply with 15% to get a new interest value = $19350 and add it to the new value ($129000) to get a new amount = $148350. and this will continue for the number of years specified.

from the example above year 1 = $60000 + $9000 = $69000 year 2 = $69000 + $60000 = $129000 ($129000*15%=$19350) now $129000+$19350 =$148350 year 3 = $148350 + $60000 = $208350 ($208350*15%=$31252.50) now $208350+$31252.50 =$239602.50 etc

i believe my explanation is plain enough. thank you.

Easiest way is to create a recursive function, assuming same yearly investment

<?php

function interest($investment,$year,$rate=15,$n=1){
    $accumulated=0;
    if ($year > 1){
            $accumulated=interest($investment,$year-1,$rate,$n);
            }
    $accumulated += $investment;
    $accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
    return $accumulated;
    }

?>

Then to run the function according to form input

<html>
<head><title>Calculate Compound Interest</title></head>
<body><h3>Calculate Compound Interest</h3>

<?php
$initial=0;
$years=0;
$rate=15;
$n=1;

if (isset($_POST['initial'])){$initial=$_POST['initial'];}
if (isset($_POST['years'])){$years=$_POST['years'];}
if (isset($_POST['rate'])){$rate=$_POST['rate'];}
if (isset($_POST['n'])){$n=$_POST['n'];}

if (isset($_POST['initial'])){
    echo "After ". $years. " years, the accumulated interest is ". interest($initial,$years,$rate,$n)."
";
}
?>

And the form input

<?php echo "<form method=\"post\" action=\""  . $_POST['SELF.PHP'] . "\">"; ?>

<p>Initial amount (contribution), $: <?php echo '<input type="number" name="initial" value='. $initial.' required/>'?> </p>
<p> Annual interest rate : <?php echo '<input type="number" name="rate" value='.$rate.' />' ?> % </p>
<p> Number of compounding periods per year? <?php echo '<input type="number" name="n" value='.$n.' />'?> </p>
<p> How many years? <?php echo '<input type="number" name="years" value='. $years.' min="1" required/>' ?></p>
<p> <input type="submit" value="Generate compound interest table."/> </p>
</form> </body> </html>

EDIT: edited to include example of running function with a form input