复利利率与定期每月存款公式,以PHP为单位


compound rate interest with regular monthly deposits formular in php

所以我试图用 php 编写一个复利公式,我有这个,但它不起作用,知道吗?

<?php
$p = 0;
$pmt = 200;
$r = 0.06;
$t = 3/12;
$n = 12;
$ans = $pmt * (((1 + $r / $n) ^ $n ($t) - 1) / ($r / $n));
echo $ans;

所以我在沙箱中尝试过,并且不断得到函数名称必须是字符串看https://3v4l.org/rdiJ7

基于该错误,如果您查看您得到的内容,您可以通过将内容间隔出来发现错误:

$ans = $pmt *  //All good
(((1 + $r / $n) //An extra bracket to start with that is not closed
 ^ //All good
$n ($t) //No operator here. 
- 1) //All good
/ ($r / $n));

所以把它改成

$ans = $pmt * ((1 + $r / $n) ^ ($n * $t - 1) / ($r / $n));

您需要一个介于 $n$t 之间的运算符。

更新

所以

P = 0 // start amount
pmt = 200 // monthly deposit
r = 0.06 // Interest amount(6%)
t = 1 // time being 1 year
n = 3/12 // investment after 3 months
ans = 606.02  ?