繁体   English   中英

php变量-从函数到可用

[英]php variables - from function to usable

我有一个PHP函数,可以执行以下操作,从CSV文件中调用数据:

// Calculates the Net Present Value based on the WAL and payment stream.

function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, &$maxdiff) {
// Calculate the WAL.
$currentWAL = calculateWAL ($paymentRowArray, $debugMode);
// Get the Low/High rate table.
$csvdata = csv_in_array ('rate_data.csv', ",", "\"", true);
// The current rate.
$currentRate = 0;
// The last line of the low/high rate table used.
$lastLineUsed = 0;

// Loop through the Low/Rate WAL table.
for ($i = 0; $i < count ($csvdata); $i++) {
    // The max diff.
    $maxdiff = $csvdata [$i]['MaxDiff'];

    if ($isLow) {
        if ($currentWAL >= $csvdata[$i]['WAL']) {
            // The WAL is higher the current rate table row.
            $currentRate = $csvdata [$i]['Low'];
            // Store the last line used.
            $lastLineUsed = $i;
            //Set the offer cost and the offer profit
            $offer_cost = 3000;
            $offer_profit = 3000;
            //Get the max difference
            $MaxDiff = $csvdata [$i]['MaxDiff'];
        }
    } else {
        if ($currentWAL >= $csvdata[$i]['WAL']) {
            // The WAL is higher the current rate table row.
            $currentRate = $csvdata [$i]['High'];
            // Store the last line used.
            $lastLineUsed = $i;
        }
    }
}

// Setup the NPV value array.
$npvValueArray = array();

// Loop through the payment stream.
for ($i = 0; $i < count ($paymentRowArray); $i++) {

    // Add the payment stream cash flow.
    $npvValueArray[] = $paymentRowArray[$i]->getCashFlow();
}

if ($debugMode) {

    echo '<table border="2">';
    echo '<tr><td><strong>WAL Bracket</strong></td>';
    echo '<td align="right">' . $lastLineUsed . '</td></tr>';
    echo '<tr><td><strong>Low/High</strong></td>';
    echo '<td align="right">' . ($isLow ? 'Low' : 'High') . '</td></tr>';
    echo '<tr><td><strong>Rate</strong></td>';
    echo '<td align="right">' . $currentRate . '</td></tr>';
    echo '<tr><td><strong>NPV</strong></td>';
    echo '<td align="right">' . round (npv (($currentRate / 12 / 100), $npvValueArray), 2) . '</td></tr>';
    $highvalue = round (npv (($currentRate / 12 / 100), $npvValueArray), 2);

    if ($isLow) {
        $lowvalue = (round (npv (($currentRate / 12 / 100), $npvValueArray), 2) - $offer_cost - $offer_profit);
        echo '<tr><td><strong>Cost of Funds</strong></td>';
        echo '<td align="right">' . $offer_cost . '</td></tr>';
        echo '<tr><td><strong>Profit</strong></td>';
        echo '<td align="right">' . $offer_profit . '</td></tr>';
        echo '<tr><td><strong>Adjusted NPV</strong></td>';
        echo '<td align="right">' . (round (npv (($currentRate / 12 / 100), $npvValueArray), 2) - $offer_cost - $offer_profit) . '</td></tr>';
        echo '<tr><td><strong>Max Difference</strong></td>';
        echo '<td align="right">' . $MaxDiff . '</td></tr>';
    }

    echo '</table><br/>';

} else {


}


// Return the Net Present Value.
return (round (npv (($currentRate / 12 / 100), $npvValueArray), -2));
//return ($maxdiff);

}

然后,我可以创建可以使用以下代码处理的变量:

$lowRate = calculateNPVByWAL ($paymentRowArray, $debugMode, TRUE, &$maxdiff);
echo 'The low rate value is $' . $lowRate .'<br>';
echo 'The maximum difference value is $' . $maxdiff .'<br>';
$adjlowRate = $lowRate - $offer_cost - $offer_profit;
echo 'The adjusted low rate value is $' . $adjlowRate .'<br>';
$highRate = calculateNPVByWAL ($paymentRowArray, $debugMode, FALSE);
echo 'The high rate value is $' . $highRate .'<br>';
$difference = $adjlowRate - $highRate;
echo 'The difference is $' . $difference .'<br>';

我的问题是$ maxdiff变量没有调用正确的值。 似乎是从CSV文件的最后一行调用该值。 任何帮助将不胜感激。

您可以在两个不同的位置将该变量赋值,如下所示:

$maxdiff = $csvdata [$i]['MaxDiff'];

这将分配当前行的MaxDiff列的值。 如果要在整个CSV上寻找最大的最大差异,则必须比较这些值。 像这样:

$realMaxDiff = 0;
if($maxdiff > $realMaxDiff) {
    $realMaxDiff = $maxdiff;
}

(您必须在定义$maxdiff 之后将其添加到某处)。


更新 -因此以上内容并不是您实际问题的解决方案

您是通过引用该函数传递$maxdiff ,因此实际上该函数运行后,其值将是最后一行的值(因为您是在进入for循环之后立即从函数内部重新分配的,因此,只是不通过引用传递,方法是从函数签名中删除&

function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, $maxdiff) {
   // function body can stay the same
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM