routine not working
Posted: Thu Nov 12, 2009 5:08 pm
Hi I have just started to learn PHP. got this from a book - and its not working. Any ideas why ? thanks
Kaps
[code = PHP]
<?php
// Title : Loan Calculator
// Author : Kaps
function ammortisationtable($pNum,$periodicPayment,$balance,$monthly_interest)
{
// calculate payment interest
$payment_interest = round($balance * $monthlyinterest,2);
//calculate payment principal
$paymentPrinicipal = round($periodicPayment - $paymentInterest,2);
//deduct obtain new balance
$newbalance = round($balance - $paymentprincipal,2);
// if new balance < monthly payment,set to zero
if ($newbalance <$paymentprincipal){
$newbalance =0;
}
printf("<tr><td>%d</td>, $pnum);
printf("<td>$%s</td>",$newbalance);
printf("<td>$%s</td>",$periodicpayment);
printf("<td>$%s</td>",$paymentprincipal);
printf("<td>$%s</td>",$paymentinterest);
// If balance not zero then recursively call amortization table()
If ($newbalance>0){
$pnum++;
amortisationtable($pNum,$periodicpayment,$newbalance,$monthlyinterest);
}
else {
return 0;
}
}
// Loan balance
$balance = 10000.0;
// Loan interest rate
$interest_rate =0.0575;
// monthly interest rate
$monthly_rate = $ interest_rate /12.0;
//Term of loan in years
$term_length =5;
//number of payments per year
$paymentsperyear = 12;
//payment iteration
$paymentnumber = 1;
//determine totalpayments
$totalpayments = $termlength * $paymentsperYearl
//determine interest component of periodic payment
$intcalc = 1 + $interestrate/$paymentsperYear;
//determine periodic payment
$periodicpayment = $round($periodicpayment,2);
//create table
echo "<table width = '50%' align='center' border = '1'>";
echo "<tr> <th> Payment number " </th></th>Balance</th> </th> Payment</th><th>Interest</th></th>Principal</th></tr>";
// call recursive function
ammortisationtable($paymentnumber,$periodicPayment,$balance,$monthlyInterest);
//close table
echo "</table>";
>
Kaps
[code = PHP]
<?php
// Title : Loan Calculator
// Author : Kaps
function ammortisationtable($pNum,$periodicPayment,$balance,$monthly_interest)
{
// calculate payment interest
$payment_interest = round($balance * $monthlyinterest,2);
//calculate payment principal
$paymentPrinicipal = round($periodicPayment - $paymentInterest,2);
//deduct obtain new balance
$newbalance = round($balance - $paymentprincipal,2);
// if new balance < monthly payment,set to zero
if ($newbalance <$paymentprincipal){
$newbalance =0;
}
printf("<tr><td>%d</td>, $pnum);
printf("<td>$%s</td>",$newbalance);
printf("<td>$%s</td>",$periodicpayment);
printf("<td>$%s</td>",$paymentprincipal);
printf("<td>$%s</td>",$paymentinterest);
// If balance not zero then recursively call amortization table()
If ($newbalance>0){
$pnum++;
amortisationtable($pNum,$periodicpayment,$newbalance,$monthlyinterest);
}
else {
return 0;
}
}
// Loan balance
$balance = 10000.0;
// Loan interest rate
$interest_rate =0.0575;
// monthly interest rate
$monthly_rate = $ interest_rate /12.0;
//Term of loan in years
$term_length =5;
//number of payments per year
$paymentsperyear = 12;
//payment iteration
$paymentnumber = 1;
//determine totalpayments
$totalpayments = $termlength * $paymentsperYearl
//determine interest component of periodic payment
$intcalc = 1 + $interestrate/$paymentsperYear;
//determine periodic payment
$periodicpayment = $round($periodicpayment,2);
//create table
echo "<table width = '50%' align='center' border = '1'>";
echo "<tr> <th> Payment number " </th></th>Balance</th> </th> Payment</th><th>Interest</th></th>Principal</th></tr>";
// call recursive function
ammortisationtable($paymentnumber,$periodicPayment,$balance,$monthlyInterest);
//close table
echo "</table>";
>