19) : $rate = 20; endif; if($rate > 1) : $rate = $rate / 100.; else : if($rate < 1) : $rate = 1. / 100.; endif; endif; $term = floatval(trim($_GET['term'])); if($term < 0.1) : $term = 1. / 12.; endif; $months = ($term>32)?$term:$term*12; $name = addslashes($_GET['name']); $year = 1; $lastpayment = 1; //:: all litteral strings provided by a client must be escaped this way :: //:: if you inject them into an HTML page :: htmlspecialchars($name); //:: filter input data to avoid all the useless/nasty cases :: //:: **MISSING :: //:: calculate the monthly payment amount :: $payment = $amount*$rate/12*pow(1+$rate/12, $term*12) / (pow(1+$rate/12, $term*12)-1); $cost = ($term*12*$payment)-$amount; //:: build the top of our HTML page :: $result = "Loan Calculator" . "" . "" . "

Loan Calculator


" . "

Dear $name, your loan goes as follows:


"; $result .= sprintf("
" . "" . "" . "" . "" . "" . "
loandetails
Amount%s
Rate%.2f%%
Term%u %s(s)
Cost%.2f (%.2f%%)
", number_format($amount, 2), $rate*100, $term, //:: **MISSING uceil(12*term), :: ($term<36)?"year":"month", $cost, 100/($amount/$cost)); $result .= sprintf("
" . "
YEAR %u
" . "" . "" . "", $year); for($month=1; $payment>0; $month++) : //:: output monthly payments :: $interest = ($amount*$rate)/12; if($amount>$payment) : $amount = ($amount-$payment)+$interest; $principle = $payment-$interest; else : //:: calculate last payment :: if($lastpayment) : $lastpayment = 0; $payment = $amount; $principle = $amount-$interest; $amount = 0; else : //:: all payments are done, just padd the table :: $amount = 0; $payment = 0; $interest = 0; $principle = 0; endif; endif; $result .= sprintf("", $month%2, $monthList[$month-1], number_format($payment, 2), number_format($interest, 2), number_format($principle, 2), number_format($amount, 2)); if($month==12) : if($amount) : $month=0; $year++; $result .= sprintf("
monthpaymentinterestprinciplebalance
%s%s" . "%s%s%s

YEAR %u" . "
" . "" . "", $year); endif; endif; endfor; //:: time the process and close the HTML page :: $result .= sprintf("
monthpaymentinterestprinciplebalance

This page was generated in %s ms." . "
(on a 3GHz CPU 1 ms = 3,000,000 cycles)" . "
", number_format((microtime(true)-$start)*1000), 2); echo $result; exit; endif; ?>