Prev | Current Page 194 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Because this function calls
itself recursively, all amortization table calculations will be performed internal to this
function; once complete, control is returned to the caller.
Listing 4-2. A Payment Schedule Calculator Using Recursion
// Loan balance
$balance = 10000.00;
// Loan interest rate
$interestRate = .0575;
// Monthly interest rate
$monthlyInterest = $interestRate / 12;
CHAPTER 4 ?–  FUNCTIONS 123
// Term length of the loan, in years.
$termLength = 5;
// Number of payments per year.
$paymentsPerYear = 12;
// Payment iteration
$paymentNumber = 1;
// Determine total number payments
$totalPayments = $termLength * $paymentsPerYear;
// Determine interest component of periodic payment
$intCalc = 1 + $interestRate / $paymentsPerYear;
// Determine periodic payment
$periodicPayment = $balance * pow($intCalc,$totalPayments) * ($intCalc - 1) /
(pow($intCalc,$totalPayments) - 1);
// Round periodic payment to two decimals
$periodicPayment = round($periodicPayment,2);
// Create table
echo "";
echo "


";
// Call recursive function
amortizationTable($paymentNumber, $periodicPayment, $balance,
$monthlyInterest);
// Close table
echo "
Payment NumberBalancePaymentInterestPrincipal
";
?>
Figure 4-1 shows sample output, based on monthly payments made on a five-year
fixed loan of $10,000.


Pages:
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206