PHPBuilder - Loan Calculator

RSS Twitter

Loan Calculator

by: Gary Taylor
September 20, 2000

Version: 1.0

Type: Full Script

Category: Money

License: GNU General Public License

Description: Small payment calculator for loans of 40 years or less

 by Gary Taylor ( of World Wide Classified, marketing
 specialist, sales and hosting.  Soon offering low cost hosting providing
 UNIX accounts supporting PHP3, MySQL, SSL, Ecommerce (no telnet access).
 Inspired by William Lucking (, where I found the
 financial function. Thanks to Richard D Foerster for eliminating those
 pesky error messages upon first loading.

 Working page at

 $per = number of periods per year.  For instance, if you are
     making payments monthly then this number is 12.
 $amount = total number of periods.  For instance, if you are compounding
     monthly for 5 years, this number would be 60 (12 months x 5 years).
 $R = annual interest rate
 $principal = current principal of a loan
 function PaymentCalc ($per,$amount,$R,$principal) {
         $Z = 1 / (1 + ($R/$per));
         return ((1 - $Z) * $principal) / ($Z * (1 - pow($Z,$amount)));

 // this echoes the input form to the browser
 echo '<CENTER><H2>Courtesy of <A HREF="">World Wide

 echo   '<TABLE><TR><TD>';

 echo   '<CENTER>Number of Payments<BR> (3 years = 36 payments)<BR>';
 echo   '<INPUT TYPE="text" NAME="amount" VALUE="'.$amount.  '"></TD>';
 echo   '<TD VALIGN=BOTTOM><CENTER>Annual Interest Rate<BR>';
 echo   '<INPUT TYPE="text" NAME="R" VALUE="'.$R. '"></TD>'; 
 echo   '<TD VALIGN=BOTTOM><CENTER>Amount Financed<BR>';
 echo   '<INPUT TYPE="text" NAME="principal" VALUE="'.$principal. '"></TD></TR>

 echo   '<TR><TD COLSPAN=2><CENTER><INPUT TYPE="submit"
 VALUE = "Press for Monthly Payment"><INPUT TYPE="reset"></FORM>';

 $pmt2= PaymentCalc(12,$amount,$R,$principal);

 echo   '<INPUT TYPE = HIDDEN NAME=amount VALUE="'.$amount. '">';
 echo   '<INPUT TYPE = HIDDEN NAME=R VALUE="'.$R. '">';
 echo   '<INPUT TYPE = HIDDEN NAME=principal VALUE="'.$principal. '">';
 echo   '<INPUT TYPE = HIDDEN NAME="Pmt2" VALUE="'.$pmt2. '">';
 echo   '<INPUT TYPE = submit VALUE = "Amortization Table">';  
 echo   '</TD></TR>';

 if($R>1){  // To accept interest rate as whole number or /100
 // To strip out commas and dollar signs.
 $pattern= '/,/';
 $pattern2= '/\$/';
 $replace= '';
 $principal = preg_replace ($pattern, $replace, $principal);
 $principal = preg_replace ($pattern2, $replace, $principal);
 if($R ==  ''){
 if($amount ==  ''){
 if($principal ==  ''){
 $pmt= PaymentCalc(12,$amount,$R,$principal);
 $Pmt = number_format($pmt, 2);  // To format into dollars and cents.

 echo  '<TR><TD COLSPAN=2>';
 echo  'Your Monthly payment will be: &nbsp; ';
 print "<FONT SIZE=+2 COLOR=RED>\$$Pmt</FONT>";
 echo   '</TD></TR></TABLE></FORM>';

 if($R ==  '0'){  // If they leave the interest rate blank
 echo  "<H2><FONT COLOR=RED>Please enter interest rate</FONT></H2>\n";
 if($amount ==  '0'){  // If they leave number of payments blank
 echo  "<H2><FONT COLOR=RED>Please enter how many payments</FONT></H2>\n";
 if($principal ==  '0'){  // If they leave amount financed blank
 echo  "<H2><FONT COLOR=RED>Please enter amount financed</FONT></H2>\n";

Comment and Contribute

Your comment has been submitted and is pending approval.

Gary Taylor



(Maximum characters: 1200). You have characters left.