Calculating with php

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
User avatar
greeneel
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:19 pm

Calculating with php

Post 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";
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
Post Reply