jackie777
asked on
PHP calculate tax function
Hi,
I have been having trouble writing a PHP function to calculate tax on an amount using an if statement.
My variables come into my page in this format. I cannot modify this and my final total must have the variable name paymentAmount
<?=$_REQUEST['SHIPPINGAMT' ]?>
<?=$_REQUEST['paymentAmoun t']?>
<?=$resArray['SHIPTOSTATE' ] ?>
The following pseudo code explains what I am trying to accomplish, but cant get the syntax correct.
If SHIPTOSTATE is equal to CA then
Subtact SHIPPINGAMT from paymentAmount multiply that total by 0.095 then add SHIPPINGAMT back to that total
I have been having trouble writing a PHP function to calculate tax on an amount using an if statement.
My variables come into my page in this format. I cannot modify this and my final total must have the variable name paymentAmount
<?=$_REQUEST['SHIPPINGAMT'
<?=$_REQUEST['paymentAmoun
<?=$resArray['SHIPTOSTATE'
The following pseudo code explains what I am trying to accomplish, but cant get the syntax correct.
If SHIPTOSTATE is equal to CA then
Subtact SHIPPINGAMT from paymentAmount multiply that total by 0.095 then add SHIPPINGAMT back to that total
note that the only variable that gets modified is $_REQUEST['paymentAmount'] . the pseudocode was a bit unclear to me, hopefully its what you want
ASKER
Thanks cdaugustin
You gave me exactly what i asked for, the only problem was what I asked for was incorrect. I needed to calculate the tax on the amount then add it to the amount. I re-wrote what you gave me below and it seems to be working how I need it. I am pretty weak on php syntax, perhaps you gan give my code a look thought to see if you think it is correct.
You gave me exactly what i asked for, the only problem was what I asked for was incorrect. I needed to calculate the tax on the amount then add it to the amount. I re-wrote what you gave me below and it seems to be working how I need it. I am pretty weak on php syntax, perhaps you gan give my code a look thought to see if you think it is correct.
$caltax = ($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.095;
if ($resArray['SHIPTOSTATE'] == CA)
{
$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);
}
You don't need to add them first, do the tax then add them
function getTax($payment, $shipping, $state) {
switch ($state)
{
case "CA": $tax_percentage = 0.095; break;
// ADD MORE CASES FOR EACH STATE HERE
}
$tax = ($payment * $tax_percentage);
return ($payment + $shipping + $tax);
}
echo getTax($_REQUEST['paymentAmount'], $_REQIEST['paymenyAmount'], resArray['SHIPTOSTATE']);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
check the atached code, i didnt understand what CA means so replace it with the apropriate value/variable.
Open in new window