PHP

Compound Interest Calculator

admin by @admin ADMIN
8m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Compute the future value of an investment compounded n times per year for t years. Returns both the final value and the interest earned.
PHP
Raw
<?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
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.