11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4 • FUNCTIONSwww.it-ebooks.info$paymentInterest = round($balance * $monthlyInterest, 2);// Calculate payment principal$paymentPrincipal = round($periodicPayment - $paymentInterest, 2);// Deduct principal from remaining balance$newBalance = round($balance - $paymentPrincipal, 2);// If new balance < monthly payment, set to zeroif ($newBalance < $paymentPrincipal) {$newBalance = 0;}printf("%d", $pNum);printf("$%s", number_format($newBalance, 2));printf("$%s", number_format($periodicPayment, 2));printf("$%s", number_format($paymentPrincipal, 2));printf("$%s", number_format($paymentInterest, 2));# If balance not yet zero, recursively call amortizationTable()if ($newBalance > 0) {$pNum++;amortizationTable($pNum, $periodicPayment,$newBalance, $monthlyInterest);} else {return 0;}}After setting pertinent variables and performing a few preliminary calculations, Listing 4-2 invokesthe amortizationTable() function. Because this function calls itself recursively, all amortization tablecalculations will be performed internal to this function; once complete, control is returned to the caller.Listing 4-2. A Payment Schedule Calculator Using Recursion

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!