if/conditionals

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Magikey
Forum Newbie
Posts: 1
Joined: Sat Apr 17, 2004 3:13 pm

if/conditionals

Post by Magikey »

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
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

try this

Code: Select all

$Cost = 2000.00; 
$Tax = 0.06; 
$Quantity = 6; 
$Discount = 25.00; 

if ($Quantity > 0) &#123; 

$Quantity = abs($Quantity); 
$Discount = abs($Discount); 
$Tax = $Tax + 1; //$Tas is now worth 1.06.
$TotalCost = (($Cost * $Quantity) - $Discount) * $Tax; 
$Payments = round ($TotalCost, 2) /12;
Note: $tax ++ results in tax being .12 NOT 1.06
Also you left off the $ for Quantity in the line $TotalCOst = blah blah
Post Reply