I think I see the pattern. Our goal is to generate a depreciation table similar to this.
Desired Output - (tab-delimited text)
Code: Select all
Year StartValue Depreciation EndingValue
2004 21297.00 4259.40 17037.60
2005 17037.60 3407.52 13630.08
2006 13630.08 2726.02 10904.06
2007 10904.06 2180.81 8723.25
2008 8723.25 1744.65 6978.60
Let's build this script incrementally. Start with a basic PHP script that does nothing but indicate that its output is plain text (instead of HTML) and display a line with four column names separated by tab characters.
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
?>
Code: Select all
Year StartValue Depreciation EndingValue
Each line of the desired output (after the column names) represents a single year in the depreciation cycle. The repetition appears to be directly related to the year, so we should write a loop based on the year. A while, do-while, or even foreach loop could be used here, but the for loop seems to be the most appropriate.
The header of the for loop contains three expressions separated by semicolons.
- A is an initialization expression that is evaluated before the loop. In our loop, we will initialize the year to be 2004. (Line 4)
- B is a conditional (test) expression that is evaluated before each iteration. If the condition is true, the loop continues through the next iteration. If it is false, the loop stops. In our loop, we will check if the year is less than or equal to 2008. (Line 4)
- C is an update expression that is evaluated after each iteration. In our loop, we will increment the year; so after the first iteration, the year will become 2005. (Line 4)
Let's update the script. The script output now shows five additional lines, one for each year. The
printf() function is used here, but echo could be used instead. The printf() format string indicates that the year is to be represented as an unsigned (non-negative) integer and followed by a newline character. (Line 5)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
for ($year = 2004; $year <= 2008; $year++) {
printf("%u\n", $year);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004
2005
2006
2007
2008
To account for the initial value of the thing being depreciated, create a variable outside the loop and assign it a value of 21297.00 (Line 4).
The value should be displayed in the output, so let's add it as an argument to printf() and add a tab separator to the format string along with "%0.2f" to indicate that the value is to be displayed as a floating-point number with two decimal places. (Line 6)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
$value = 21297.00;
for ($year = 2004; $year <= 2008; $year++) {
printf("%u\t%0.2f\n", $year, $value);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004 21297.00
2005 21297.00
2006 21297.00
2007 21297.00
2008 21297.00
That the value of the object stays the same for each year is, of course, incorrect. So let's calculate the amount of depreciation for each year.
To calculate the amount of depreciation, we multiply the current value by 0.2 (20%). Let's first try putting the expression "$value * 0.2" directly into printf() to display it as a float just as we did with the value. (Line 6)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
$value = 21297.00;
for ($year = 2004; $year <= 2008; $year++) {
printf("%u\t%0.2f\t%0.2f\n", $year, $value, $value * 0.2);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004 21297.00 4259.40
2005 21297.00 4259.40
2006 21297.00 4259.40
2007 21297.00 4259.40
2008 21297.00 4259.40
While this works, we will need to use the amount of depreciation to calculate the ending value; so, instead of writing the multiplication expression twice, let's assign its value to a new variable. The variable assignment goes
inside the loop because its value needs to change on each iteration. (Line 6)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
$value = 21297.00;
for ($year = 2004; $year <= 2008; $year++) {
$depreciation = $value * 0.2;
printf("%u\t%0.2f\t%0.2f\n", $year, $value, $depreciation);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004 21297.00 4259.40
2005 21297.00 4259.40
2006 21297.00 4259.40
2007 21297.00 4259.40
2008 21297.00 4259.40
We have calculated the depreciation, but the current value will not change until we subtract the depreciation from it. (Line 6)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
$value = 21297.00;
for ($year = 2004; $year <= 2008; $year++) {
$depreciation = $value * 0.2;
$value = $value - $depreciation;
printf("%u\t%0.2f\t%0.2f\n", $year, $value, $depreciation);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004 17037.60 4259.40
2005 13630.08 3407.52
2006 10904.06 2726.02
2007 8723.25 2180.81
2008 6978.60 1744.65
There is a problem now. While the value is depreciating correctly, the number that appears in the StartValue column is the number that should be in the EndingValue column. We could fix this a number of ways.
- We could add the depreciation back into the StartValue column and put the expression "$value + $depreciation" in printf(), but that would mean an extra arithmetic operation.
- We could print the year and the value before subtracting the depreciation, but then two printf() statements would be required to display each line.
- In order to avoid the extra arithmetic operation and use a single printf() statement, the old value must be stored in another variable before the subtraction. (Line 7) The old value is then used for the StartValue argument in printf() and an EndingValue argument is added to printf() for the depreciated value. (Line 8)
depreciation-table.php
Code: Select all
<?php
header('Content-Type: text/plain');
echo "Year\tStartValue\tDepreciation\tEndingValue\n";
$value = 21297.00;
for ($year = 2004; $year <= 2008; $year++) {
$depreciation = $value * 0.2;
$old_value = $value;
$value = $value - $depreciation;
printf("%u\t%0.2f\t%0.2f\t%0.2f\n", $year, $old_value, $depreciation, $value);
}
?>
Code: Select all
Year StartValue Depreciation EndingValue
2004 21297.00 4259.40 17037.60
2005 17037.60 3407.52 13630.08
2006 13630.08 2726.02 10904.06
2007 10904.06 2180.81 8723.25
2008 8723.25 1744.65 6978.60
We have reached our goal of creating a script that is capable of generating a depreciation table for years 2004-2008 given an initial value of 21297.00 and a depreciation rate of 20% per year, but there is still room for improvement. This script is not as flexible as it could be by adding a few more variables for starting year, ending year, and depreciation rate to allow a user to interact with it.
Edit: This post was recovered from search engine cache.