# How to Create Compound Interest Calculator Using PHP?

Today I will tell you how you can craete Compound Interest Calculator Using PHP? First of all, you create an html form, add text field and 1 button. I will tell you explain step by step how you can craete Compound Interest Calculator Using PHP For this you can see in the example mentioned below.

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

`<?phpfunction 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)."\n";}?>`

And the form input

`<form method="post" action="/a/compound.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>`

You can add whole code to compound-calculator.php by creating a file.

compound-calculator.php

`<?phpfunction 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;}?><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)."\n";}?><form method="post" action="/a/compound.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>`