Use a Free Mortgage Calculator in a Web Page

How to Calculate Compound Interest Loan Details with PHP

© Mark Alexander Bain

Aug 19, 2009
Use a Free Mortgage Calculator in a Web Page, Mark Alexander Bain
A web page developer can create their own mortgage calculator with PHP. Their users will be able to calculate monthly payments or how large a mortgage they can afford

Successful web site designers are always looking for ways in which to add value to their web pages for their users. Given the current economic climate and the efforts that many governments are making in aiding the home owner, a free mortgage calculator is a useful addition to any web site. Fortunately this task can be carried out easily by using a few PHP functions and some standard formulae. This PHP application could, for example, be able to:

  • calculate monthly repayments given the initial loan amount, interest rates and length of the mortgage
  • calculate a mortgage given a monthly budget instead of the initial loan amount

The first step, therefore, is to create a PHP function that calculates the monthly repayment for the mortgage.

A PHP Function for Calculating Monthly Payments for Mortgages

The general formula for calculating monthly repayment for a mortgage (or to more exact any compound interest loan) is discussed in <a href="http://computerprogramming.suite101.com/article.cfm/a_simple_home_mortgage_calculator">A Simple Home Mortgage Calculator</a>. This formula requires 3 inputs:

  • the amount borrowed
  • the interest rate
  • the period for which the mortgage is to run

These can be used as the input to the function which carries out the calculation:

function mp ($amount, $interest, $years) {
$payments = $years * 12; //Total number of months
$percentage = $interest / 100 / 12; //Monthly interest rate
return $amount * ( $percentage * pow(1 + $percentage, $payments) ) / ( pow(1 + $percentage, $payments) - 1);
}

The function can then be tested by using:

echo "$" . round(mp (100000, 5, 25),2) . "<br>\n";

This will return the monthly repayment for a mortgage of £100000 over 25 years at an interest rate of 5%, and the result can be seen in Figure 1. The next stage is to create a function for calculating a mortgage when given a set budget.

A PHP Function for Calculating Affordable Mortgages

The mortgage function is nearly the same as the monthly repayment function. It's just a matter of transposing the formula:

function mortgage ($mp, $interest, $years) {
$payments = $years * 12; //Total number of months
$percentage = $interest / 100 / 12; //Monthly $interest rate
return $mp / (( $percentage * pow(1 + $percentage,$payments) ) / ( pow(1 + $percentage, $payments) - 1));
}

And it can be tested by using:

echo "$" . round(mortgage (600, 5, 25),2) . "<br>\n";

Figure 2 shows the result of running this.

Creating a User Interface for the Mortgage Calculator

The application user must, of course, be able to choose their own inputs and to decide whether they want to view a mortgage or a monthly payment. These inputs can be incorporated into an HTML form:

<form method=post>
<table>
<tr><td>Amount:</td><td>$<input name=amount></td></tr>
<tr><td>Interest Rate:</td><td><input name=interest>%</td></tr>
<tr><td>Number of Years:</td><td><select name=years>
<option>10
<option>15
<option>20
<option>25
<option>30
</select></td></tr>
<tr><td colspan=2>
<input type=radio name=choice value=mp checked>Monthly Payment
<input type=radio name=choice value=mortgage>Mortgage</td></tr>
<tr><td colspan=2><input type=submit value="Send Data"></td></tr>
</table>
</form>

And then some more PHP code is required. This will run the appropriate function according to the selections made by the application user:

<?php
if ($_REQUEST['amount'] != "") {
if ($_REQUEST['choice'] == "mortgage") {
echo "$" . round(mortgage ($_REQUEST['amount'], $_REQUEST['interest'], $_REQUEST['years']),2) . "<br>\n";
} else {
echo "$" . round(mp ($_REQUEST['amount'], $_REQUEST['interest'], $_REQUEST['years']),2) . "<br>\n";
}
}
?>

And with just those few lines of code the PHP programmer will have created a mortgage calculator that they can use in their web sites to provide their users with a valuable service.


The copyright of the article Use a Free Mortgage Calculator in a Web Page in PHP Programming is owned by Mark Alexander Bain. Permission to republish Use a Free Mortgage Calculator in a Web Page in print or online must be granted by the author in writing.


Use a Free Mortgage Calculator in a Web Page, Mark Alexander Bain
Figure 1: A PHP Function for Mortgage Repayments, Mark Alexander Bain
Figure 2: A PHP Function to Calculate Mortgages, Mark Alexander Bain
Figure 3: A Mortgage Calculator, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo