Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:01:18+00:00 2026-06-12T11:01:18+00:00

I have a PHP function that does the following, calling data from a CSV

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:01:18+00:00Added an answer on June 12, 2026 at 11:01 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that extract dat of an invoice from DB. The
I have a function in jQuery that does something like the following to allow
At this present point in time I have some code that does the following:
I have a php function I wrote that will take a text file and
I have a php file (php1.php). Within that php file i have the following
I have a PHP function that builds a JSON array via $jsonArray= array(); for
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a PHP function that requires can take 3 parameteres... I want to
I have a PHP function that I want to make available publically on the
Is it possible to have a PHP function that is both recursive and anonymous?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.