本金固定存入某段时间
function getMoney($money,$time=365,$lilv=0.04){
//$money 本金,$time 收益天数,$lilv 利率
$money=$money * $lilv / 360 + $money;
// 一天的收益按照年收益除以360来算
$time--;
if($time>0){
$money=getMoney($money,$time);
}
return $money;
}
$re=getMoney(10000,365*1);
var_dump($re);==================================在初始本金下,每月存入固定值计算优化
function getMoney($money,$add,$time=365,$month=30,$lilv=0.04){
//$money 本金,$add 每月新存入,$time 收益天数,$month 一个月天数,$lilv 利率
$money=$money * $lilv / 360 + $money;
// 一天的收益按照年收益除以360来算
$time--;
$month--;
if($time>0){
if($month==0){
//每过一个月本金加新存入
$month=30;
$money=getMoney($money+$add,$add,$time,$month);
}else{
$money=getMoney($money,$add,$time,$month);
}
}
return $money;
}
$re=getMoney(10000,1000,365*1);
var_dump($re);==================================后来发现递归如果层级多了,十分消耗内存资源,能用迭代不要用递归,一般知道需要循环多少层用for循环即可,例如上面循环条件是时间,而时间是由我们确定的变量,所以可以用for来代替:
function income($capital,$lilv=0.04){
//$capital本金,$lilv 利率
$money=$capital*$lilv/365+$capital;//本金乘以利率除以一年时间,再加上本金;
return $money;
}
function count_income($money,$time=365){
//$time 存储时间,天;
for($i=0;$i<$time;$i++){
$money=income($money);
}
return $money;
}
var_dump(count_income(10000,365));这样更加节省资源; 