<?php
function compoundInterest(
float $principal,
float $annualRate,
int $years,
int $timesPerYear = 12
): array {
$final = $principal * (1 + $annualRate / $timesPerYear) ** ($timesPerYear * $years);
return [
'final' => round($final, 2),
'interest' => round($final - $principal, 2),
];
}
print_r(compoundInterest(10_000, 0.07, 10));
// [ final => 20096.61, interest => 10096.61 ] ← $10k at 7% APR for 10 years, monthly
Create a free account and build your private vault. Share publicly whenever you want.