Q:

php variables - from function to usable

I have a PHP function that does the following, calling data from a CSV file:
// 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);

}

Then I can create variables that I can manipulate using this code:
$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>';

My problem is that the $maxdiff variable doesn't call the correct value. It seems to call the value from the last line in the CSV file. Any help would be greatly appreciated.

A:

You assign to that variable on two different places, like this:
$maxdiff = $csvdata [$i]['MaxDiff'];

This assigns the value of the current row's MaxDiff column. If you're looking for the highest max diff on the whole CSV, you have to compare the values. Something like this:
$realMaxDiff = 0;
if($maxdiff > $realMaxDiff) {
    $realMaxDiff = $maxdiff;
}

(You have to add that somewhere after $maxdiff has been defined).

UPDATE - so the above was not the solution to your actual problem
You're passing $maxdiff by reference to the function, so in fact after the function runs its value will be the one from the last row (since you reassign from inside the function, right after you enter the for loop. To fix that, just don't pass by reference, by removing the & from the function signature:
function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, $maxdiff) {
   // function body can stay the same
}

