Retirement Planner App

This project shows you how to use a while loop to determine how long it will take to reach the amount of money you need to retire. This app takes the values for how much money you are starting with, how much you will add per year, your expected interest rate, and how much you need to retire. The while loop adds the interest and money added every year and prints a line until the total amount is above the amount you need to retire.

Code Explanation:

Lines 4-7 – Take the values from the HTML form and create PHP variables

Lines 9-15 – PHP dynamically writes the HTML form. We use PHP for this project so that we can set default values for the form fields. This makes it easier for users to quickly change specific numbers, and to quickly verify what values they submitted in the form to get the current results. value=$variable sets the field value to the variable value.

Line 17 – Creates a variable for the Year number so that we can track it in the while loop

Line 18 – Creates a variable for Current Worth so that we can print out what the total worth is every year, and sets it to the value of $starting.

Line 19 – Creates a variable for the interest rate by dividing what the user gives by 100 so we can use this in the wile loop. 7% turns into .07

Line 21 – An if conditional is used to verify the interest, starting, and need filed values are not blank. If these fields are blank it could create an endless loop and lock the app.

Lines 22-24 – Create the HTML table format for printing out the data. This makes it easier for users to make sense of what is printed. “——-” were added to quickly create extra padding for the table columns.

Line 25 – Create a while loop where the loop continues as long as the current worth is under the value for the amount needed.

Line 26 – Calculates the interest earned for the year

Line 27 – Adds current worth, yearly contribution, and interest to set the value for the current worth.

Line 28 – Prints out a table row with the year, current worth, and interest earned for the year.

Line 30 – Increments the year variable by one.

Line 32 – Closes the HTML table

<h1>Retirement Planner</h1>

$starting = $_POST['starting'];
$yearly_contribution = $_POST['yearly_contribution'];
$apr = $_POST['apr'];
$need = $_POST['need'];

echo "<form action='retirement.php' method='post'>";
echo "Starting Amount <input type='text' name='starting' value=$starting><br>";
echo "Amount per Year <input type='text' name='yearly_contribution' value=$yearly_contribution><br>";
echo "Interest Rate <input type='text' name='apr' value=$apr>%<br>";
echo "Retirement Amount <input type ='text' name='need' value=$need><br>";
echo "<input type='submit'>";
echo "</form>";

$year = 1;
$current_worth = $starting;
$interest_rate = $apr / 100;

if($apr !=" " && $starting !=" " && $need !=" "){
    echo "<table>";
    echo "<tr><th>Year ----</th><th>Current Value ----------</th><th>Interest ----------</th></tr>";
    echo "<tr><td>Start</td><td>$starting</td></tr>";
    while($current_worth < $need){
        $interest = ($yearly_contribution + $current_worth) * $interest_rate;
        $current_worth = $yearly_contribution + $current_worth + $interest;
        echo "<tr><td>$year</td><td>$current_worth</td><td>$interest</td></tr>";

    echo "</table>";
Code language: HTML, XML (xml)