Ok, so I finally figured out how to make a function that returns the monthly payment on a home mortgage based on the amortization formula. I had a real rough time with the order of operations and I am curious as to shortcuts etc.
I basically had to break my formula down into small pieces based on the proper order of operations. Why can't I just put a whole formula in there and have php output the proper figures? Or is it a flaw with php?
For example I could not just divide the numerator by the denominator (separating the code with parenthesis) cause it would return 0.
Anyway, thanks for the input and helping me understand how PHP works:
<?php
/*
First you must define some variables to make it easier to set up:
# P = principal, the initial amount of the loan
# I = the annual interest rate (from 1 to 100 percent)
# L = length, the length (in years) of the loan, or at least the length over
which the loan is amortized.
The following assumes a typical conventional loan where the interest is
compounded monthly. First I will define two more variables to make the
calculations easier:
# J = monthly interest in decimal form = I / (12 x 100)
# N = number of months over which loan is amortized = L x 12
Okay now for the big monthly payment (M) formula, it is:
J
M = P x ------------------------
1 - ( 1 + J ) ^ -N
M = P * ( J / (1 - (1 + J) ** -N))
*/
function calcMonthlyPayment($amount, $int, $term)
{
$I = $int; /*I understand this piece was unnecessary however, it helped me keep my ducks in a row if you know what i mean :) */
$J = $I/1200;
//amount of loan
$P = $amount;
//Length of Note in months
$N = $term * (-12);
//breaking the denominator up into pieces for order of operations
$base = 1+$J;
$base = pow($base, $N);
$base = 1 - $base;
//completing the formula
$M = $P * ($J) / ($base);
$M = number_format ($M, 2);
return $M;
}
$check = calcMonthlyPayment(200000, 5.78, 30);
echo "<p>$check</p>";
?>
Thanks for the help!
Warmaster