Page 1 of 1

Calculating with php

Posted: Fri Aug 15, 2003 5:49 pm
by greeneel
hello,

I`m having some problems i added the following to a form that i`m working on just starting out in php and it doesn`t work the way i would like .. I don`t get any output from it the prices aren`t calculated as the book that i`m usung stated their is 0 in items ordered , $0.00 in subtotal and $0.00 in total including tax..

I used $HTTP_POST_VARS[] to echo the values from the form.. any ideas to help i think it may be because i`m using php 4.3.2 and the book is and old book?


define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPRICE", 4);

$totalqty = $tireqty + $oilqty + $sparkqty;
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2);
echo "<br>\n";
echo "Items ordered: ".$totalqty."<br>\n";
echo "Subtotal: $".$totalamount."<br>\n";
$taxrate = 0.10; // local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
$totalamount = number_format($totalamount, 2);
echo "Total including tax: $".$totalamount."<br>\n";

Posted: Fri Aug 15, 2003 7:29 pm
by Coco
number_format() returns a string not a numerical value.
and you cant put $ inside " ". Either use ' ' or escape the $ with a \
ie

Code: Select all

<?php
$totalqty = $tireqty + $oilqty + $sparkqty; 
$totalamount = $tireqty * TIREPRICE 
+ $oilqty * OILPRICE 
+ $sparkqty * SPARKPRICE; 
echo '<br>\n'; 
echo 'Items ordered: '.$totalqty.'<br>\n'; 
echo 'Subtotal: $'.number_format($totalamount, 2).'<br>\n'; 
$taxrate = 0.10; // local sales tax is 10% 
$totalamount = $totalamount * (1 + $taxrate); 
echo 'Total including tax: $'.number_format($totalamount, 2).'<br>\n';

?>
Wont necessarily completely fix your problems, but i cant see it hurting