Jump to content

Catch-22 calculating in PHP


JasonGreen

Recommended Posts

There doesn't seem to be a way around this. I want to be able to calculate values using variables but in such a way that the value continues to change - however, I cannot simply write out the number, I need a variable.

 

Here is the problem, look at my example below, if I were to use calculations based on define variable, I would end up using longer and longer calculations and that would be a horible mess.

 

Like this

 

print $serveryear . "      " . $servercost . "              " . ($servercost * HIGH_DEPREC) . "            " . ($servercost - $servercost * HIGH_DEPREC) . "\n";

print ($serveryear + 1) . "      " . ($servercost - $dodgecost * HIGH_DEPREC) . "      " . ($servercost - $servercost * HIGH_DEPREC * HIGH_DEPREC) . "      " . ETC . "\n";

It just grows and grows!!!!

 

At the same time I cannot define a variable for each and every calculation, that too is a mess that grows and grows.

 

DELL SERVER– Depreciation over 5-year period (Factor:  .20)

 

YEAR        Start Value      Deprec      Ending Value

 

2004          21297.00        4259.40    17037.60

 

2005          17037.60        3407.52    13630.08

2006

2007

2008

 

Any insights, comments, and suggestions will be greatly appreaciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/174085-catch-22-calculating-in-php/
Share on other sites

Jason,

 

I think you need to use a function, something like this, then you can pass any values to it:

 

<?php

ShowDep("DELL SERVER", 21297, 2004, 5, 20);

function ShowDep($title, $servercost, $serveryear, $numYears, $depRate){
    $startValue = $servercost;
    $depValue = 0;
    $endValue = 0;
    print $title . "<br/>";
    for ( $counter = 0; $counter < $numYears; $counter += 1) {
        $depValue = $startValue * $depRate / 100;
        $endValue = $startValue - $depValue;
        print $serveryear + $counter . " --- " .
              $startValue  . " --- " .
              $depValue  . " --- " .
              $endValue  . "<br/>";
        $startValue = $endValue;
    }
}

?>

 

I've only just started to learn PHP, so there's probably a better way to do it. Also, the output is untidy as I've not figured out the print statement yet!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.