簡體   English   中英

如何在PHP中像這樣增加變量

[英]How to increment a variable like so in PHP

我有這個劇本...

//initial value

$fund = 10;
$rebate = 0.01; // constant 1 percent
$maturity = 30; //days

echo '<ul>';
echo '<li>Day1 fund: $' . $fund . '</li>';

//day1 value
$fund = incfund($fund); //should return $10.10
echo '<li>Day2 fund: $' . $fund . '</li>';

//day2 value
$fund = incfund($fund); //should return $10.20
echo '<li>Day3 fund: $' . $fund . '</li>';

//day3 value
$fund = incfund($fund); //should return $10.30
echo '<li>Day4 fund: $' . $fund . '</li>';

echo '</ul>';
//continue increasing the fund value daily until day 30...


//function to increase $fund

function incfund($x){
for($i = 0; $i<=$maturity; $i++){
$newfund = $x + $rebate;
$fund = $newfund;
return $fund;
}
}

當前,如果我運行腳本,它將輸出:

Day1 fund: $10
Day2 fund: $10
Day3 fund: $10
Day4 fund: $10

我真正想要輸出的時間是:

Day1 fund: $10.10
Day2 fund: $10.20
Day3 fund: $10.30
Day4 fund: $10.40
...
...
Day30 fund: $13.00

基本上,我想做的是計算1%的回扣並將其復合到原始基金中,然后繼續復合直到到期日。

為什么我不能做我想做的事呢?

看你的代碼

function incfund($x){
    for($i = 0; $i<=$maturity; $i++){
        $newfund = $x + $rebate;
        $fund = $newfund;
        return $fund;
    }
}

它到底在做什么

function incfund($x){
    return $x+$rebate;
}

你應該改變什么

function incfund($x){
    global $rebate;
    return $x+$rebate;
}

要么

function incfund($x, $rebate){
    return $x+$rebate;
}

並更改視圖層

for ($i = 1; $i <= $maturity; $i++, $fund = incfund($fund)) {
    echo '<li>Day'.$i.' fund: $' . $fund . '</li>';
}

但是當您描述功能時,所有代碼應該看起來像

//initial value

$fund = 10;
$rebate_weekdays = 0.01; // constant 1 percent
$rebate_weekend = 0.02; // constant 1 percent
$maturity = 30; //days

echo '<ul>';

// show fund and continue increasing the fund value daily until day 30...
for ($i = 1; $i <= $maturity; $i++) {
    $rebate = (is_it_weekend($i)) ? $rebate_weekend: $rebate_weekdays;
    $curfund = incfund($fund, $i, $rebate);
    echo '<li>Day'.$i.' fund: $' . $curfund . '</li>';
}

echo '</ul>';

//function to calculate current $fund    
function incfund($fund, $x, $rebate){
    return $fund * (1 + $x * $rebate);
}

如何查看當天可以找到的日期。 它只是使用http://php.net/mktimehttp://php.net/date

$rebate$maturity變量作為函數參數傳遞。

function incfund($x,$rebate,$maturity){
...
}

通話功能:

incfund($fund,$rebate,$maturity);

此外,如果要格式化為小數點后兩位數字,則應使用number_format($number,2)

除了GBD的答案

如果您真的要這樣做,這就是您可以更改方法以執行所需操作的方法

function incfund($x){
 global $maturity;
 global $fund;
 global $rebate;
  for($i = 0; $i<=$maturity; $i++){
     $newfund = $x+($x*$rebate);
     $newfund=number_format((float)$newfund, 2, '.', '');
     $fund = $newfund;
    return $fund;
     }
  }

這對我有用,並輸出您想要的。 我發現更容易在循環中創建一天。

<?php

//function to increase $fund

function incfund(){
        $fund = 10;
        $rebate = 1; // constant 1 percent
        $maturity = 30; //days
        $html = '';
        $interest = ($fund / 100) * $rebate;
        for($i = 0; $i<=$maturity; $i++){
            $realInterest = $interest * $i;
            $html .= '<li>Day'.$i.' fund: $' . number_format($fund+$realInterest ,2) . '</li>';
        }
    echo $html;
}

echo incfund();
?>


Day0 fund: $10.00
Day1 fund: $10.10
Day2 fund: $10.20
Day3 fund: $10.30
Day4 fund: $10.40
Day5 fund: $10.50
Day6 fund: $10.60
Day7 fund: $10.70
Day8 fund: $10.80
Day9 fund: $10.90
Day10 fund: $11.00
Day11 fund: $11.10
Day12 fund: $11.20
Day13 fund: $11.30
Day14 fund: $11.40
Day15 fund: $11.50
Day16 fund: $11.60
Day17 fund: $11.70
Day18 fund: $11.80
Day19 fund: $11.90
Day20 fund: $12.00
Day21 fund: $12.10
Day22 fund: $12.20
Day23 fund: $12.30
Day24 fund: $12.40
Day25 fund: $12.50
Day26 fund: $12.60
Day27 fund: $12.70
Day28 fund: $12.80
Day29 fund: $12.90
Day30 fund: $13.00

好的,因為您是從基本基金中計算出結果,所以您必須保留此值。

應該這樣做:

// initial
$fund = 10;
$rebate = 0.01; // constant 1 percent
$maturity = 30; //days

incfunds($fund, $rebate, $maturity);

function incfunds($fund, $rebate, $maturity) {
    echo "<ul>";
    for($i = 0; $i < $maturity; $i++) {
        echo "<li>Day{$i+1} fund:".($fund + (($fund/100) * $i*$rebate))."</li>";
    }
    echo "</ul>";
}

就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM