|
||||||
Use a Free Mortgage Calculator in a Web PageHow to Calculate Compound Interest Loan Details with PHPA 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:
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 MortgagesThe 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:
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 MortgagesThe 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 CalculatorThe 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.
|
||||||
|
|
||||||
|
|
||||||