if/conditionals
Posted: Sat Apr 17, 2004 3:13 pm
Hello everyone. I am new to PhP and have been using PhP for the World Wide Web along with a course I am taking to learn. I've run across a problem. I will print the script first, and then the results I received.
-----------------------------------------------------------------------------------
<?php
/* $Quantity must be passed to this page from a form or via the URL. $Discount is optional. */
$Cost = 2000.00;
$Tax = 0.06;
$Quantity = 6;
$Discount = 25.00;
if ($Quantity) {
$Quantity = abs($Quantity);
$Discount = abs($Discount);
$Tax++; //$Tas is now worth 1.06.
$TotalCost = (($Cost * Quantity) - $Discount) * $Tax;
$Payments = round ($TotalCost, 2) /12;
//Pring the results.
print ("You requested to purchase $Quantity widget(s) at \$$Cost each. \n<p>");
print ("The total with tax, minus your \$$Discount, comes to $");
printf ("%01.2f" , $TotalCost);
print (".\n<p>You may purchase the widget(s) in 12 monthly installments of $");
printf ("%01.2f" , $Payments);
print (" each.\n<p>");
}
?>
-----------------------------------------------------------------------------------
THE RESULTS
----------------------------------------------------------------------------------
You requested to purchase 6 widget(s) at $2000 each.
The total with tax, minus your $25, comes to $-26.50
You may purchase the widget(s) in 12 monthly installments of $-2.21
----------------------------------------------------------------------------------
As you can see, the calculations are totally wrong. I followed the code character for character. Can someone please tell me what is wrong with this picture?
Magikey
-----------------------------------------------------------------------------------
<?php
/* $Quantity must be passed to this page from a form or via the URL. $Discount is optional. */
$Cost = 2000.00;
$Tax = 0.06;
$Quantity = 6;
$Discount = 25.00;
if ($Quantity) {
$Quantity = abs($Quantity);
$Discount = abs($Discount);
$Tax++; //$Tas is now worth 1.06.
$TotalCost = (($Cost * Quantity) - $Discount) * $Tax;
$Payments = round ($TotalCost, 2) /12;
//Pring the results.
print ("You requested to purchase $Quantity widget(s) at \$$Cost each. \n<p>");
print ("The total with tax, minus your \$$Discount, comes to $");
printf ("%01.2f" , $TotalCost);
print (".\n<p>You may purchase the widget(s) in 12 monthly installments of $");
printf ("%01.2f" , $Payments);
print (" each.\n<p>");
}
?>
-----------------------------------------------------------------------------------
THE RESULTS
----------------------------------------------------------------------------------
You requested to purchase 6 widget(s) at $2000 each.
The total with tax, minus your $25, comes to $-26.50
You may purchase the widget(s) in 12 monthly installments of $-2.21
----------------------------------------------------------------------------------
As you can see, the calculations are totally wrong. I followed the code character for character. Can someone please tell me what is wrong with this picture?
Magikey