Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
select version() as Version;
<?php
/**
* Generates the amortization schedule as a table with annual schedule.
*
* @param float $loanAmount The loan amount in USD.
* @param float $interestRate The interest rate in percentage.
* @param int $loanTerm The loan term in months.
*
* @return string The HTML table representing the amortization schedule.
*/
function generateAmortizationScheduleAnnual(float $loanAmount, float $interestRate, int $loanTerm): string {
$html = '<table>';
$html .= '<tr><th>Year</th><th>Interest</th><th>Principal</th><th>Ending Balance</th></tr>';
$monthlyInterestRate = $interestRate / 12 / 100;
$monthlyPayment = ($loanAmount * $monthlyInterestRate) / (1 - pow(1 + $monthlyInterestRate, -$loanTerm));
$year = 1;
$remainingBalance = $loanAmount;
while ($remainingBalance > 0) {
$interest = $remainingBalance * $monthlyInterestRate;
$principal = $monthlyPayment - $interest;
$endingBalance = $remainingBalance - $principal;
$html .= '<tr>';
$html .= '<td>' . $year . '</td>';
$html .= '<td>' . $interest . '</td>';
$html .= '<td>' . $principal . '</td>';
$html .= '<td>' . $endingBalance . '</td>';
$html .= '</tr>';
$remainingBalance = $endingBalance;
$year++;
}
$html .= '</table>';
return $html;
}
// Example usage
$loanAmount = 20000;
$interestRate = 5;
$loanTerm = 60;
$amortizationSchedule = generateAmortizationScheduleAnnual($loanAmount, $interestRate, $loanTerm);
echo $amortizationSchedule;
?>