Greetings,Please have patience with this newbie.I am trying to learn PHP and have run into a seemingly bizarre problem.I am trying to execute a simple tax calculation and am getting inconsistent results.The server is running PHP 4.4.1 with Zend Engine v1.3.0 on a Linux (2.4.27?) server with Apache 1.3.33.You can access my test page here: Bob's Bits. If subtotal is less than $1000.00, the 10% tax is correctly calculated. Any subtotal above that does not calculate correctly. Try with 9 tires then with 10 tires as values. I have tried many ways of restructuring the statements to no avail. I have tried using 10 for $taxrate then dividing by 100 in the tax calculation. I have tried removing the decimal completely, but the calculation still performs the same way (Subtotal $900 gets $9000.00 tax, subtotal $1000 gets $10.00 tax). I have tried removing the number_format function. The portion of code that seems affected is:
$taxrate=.10;echo 'Subtotal: $'.number_format($subtotal,2).'<br>';echo 'Tax Rate: '.($taxrate*100).'%<br>';echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>';echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>';
The subtotal always calculates correctly.I did create a separate php page with a hard coded subtotal amount in the calculation that works properly. that can be seen here:Calc Test.The code for that is:
$subtotal=1100;$taxrate=.10;echo 'Subtotal: $'.number_format($subtotal,2).'<br>';echo 'Tax Rate: '.($taxrate*100).'%<br>';echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>';echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>';
I appreciate any assistance with this.Just in case it is being caused by something outside that code block and you are actually still reading this, here is the full code:
<html> <head> </head> <title> Bob's Bits - Order Results </title> <body> <h1> Bob's Bits </h1> <h2> Order Results </h2> <?php // Create and load short variable names $tireqty = $_POST['tireqty']; $oilqty = $_POST['oilqty']; $sparkqty = $_POST['sparkqty']; // Calculate total items ordered - this will also be used to validate entry $totalqty = 0; $totalqty = $tireqty + $oilqty + $sparkqty; // Create and assign constants for pricing DEFINE ('tireprice', 100.00); DEFINE ('oilprice', 2.00); DEFINE ('sparkprice', 1.00); // Temporarily displays status of variable fields using isset /* echo 'isset($tireqty): '.isset($tireqty).'<br />'; echo 'isset($oilqty): '.isset($oilqty).'<br />'; echo 'isset($sparkqty): '.isset($sparkqty).'<br />'; echo 'empty($tireqty): '.empty($tireqty).'<br />'; echo 'empty($oilqty): '.empty($oilqty).'<br />'; echo 'empty($sparkqty): '.empty($sparkqty).'<br />'; echo 'Variable totalqty= '.$totalqty.'<br />'; */ // Display results if( $totalqty == 0 ) // No items were ordered so display an error message { echo '<font color=red />'; echo 'You did not order anything on the previous page! <br />'; echo '</font>'; } else // At least one item was ordered so display order details { echo '<p>Order Processed at '.date('H:i').' on '.date(' F jS Y').'</p>'; if ($tireqty > 0) { /* Incorporate a volume discount algorithm for tires such that: 1 - 10 tires = 0% discount 10 - 49 tires = 5% discount 50 - 99 tires = 10% discount 100 or more tires = 15% discount */ if ($tireqty < 10) $discount = 0; elseif ($tireqty > 9 && $tireqty < 50) $discount = 0; // should be 5 elseif ($tireqty > 49 && $tireqty < 100) $discount = 10; elseif ($tireqty > 99) $discount = 15; echo 'Tires: '.$tireqty.'@ $'.number_format(tireprice,2).' each: $'.number_format($tiretotal=($tireqty*(tireprice -(tireprice*$discount/100))),2).'<br>'; } if ($oilqty > 0) echo 'Oil: '.$oilqty.'@ $'.number_format(oilprice,2).' each: $'.number_format($oiltotal=($oilqty*oilprice),2).'<br>'; if ($sparkqty > 0) echo 'Spark Plugs : '.$sparkqty.'@ $'.number_format(sparkprice,2).' each: $'.number_format($sparktotal=($sparkqty*sparkprice),2).'<br>'; echo 'Subtotal: $'.$subtotal=number_format(($tiretotal+$oiltotal+$sparktotal),2).'<br>'; $taxrate=.10; echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>'; echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>'; echo 'Total items: '.($tireqty + $oilqty + $sparkqty); } ?> <p>Page 47</p> </body></html>