Solved

PHP calculate tax function

Posted on 2009-05-05
6
941 Views
Last Modified: 2012-05-06
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['paymentAmount']?>
<?=$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

0
Comment
Question by:jackie777
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24310222
hi
check the atached code, i didnt understand what CA means so replace it with the apropriate value/variable.
if ($resArray['SHIPTOSTATE'] == CA)

{

	$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.095 + $_REQUEST['SHIPPINGAMT'];

	

	

}

Open in new window

0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24310232
note that the only variable that gets modified is $_REQUEST['paymentAmount'] . the pseudocode was a bit unclear to me, hopefully its what you want
0
 

Author Comment

by:jackie777
ID: 24310327
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.
$caltax = ($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.095;
 

if ($resArray['SHIPTOSTATE'] == CA)

{

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);               

}

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Expert Comment

by:nplib
ID: 24310330
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']);

Open in new window

0
 
LVL 5

Accepted Solution

by:
cdaugustin earned 500 total points
ID: 24310428
hi,

im glad I could help. The only thing that you need to "fix" on your code would be

if ($resArray['SHIPTOSTATE'] == CA)


in this case if CA is not a defined constant then replace it with 'CA' to make the comparison clear (if you leave it like this it will work, but it will output an constant undefined warning and will do a implicit conversion to string, which you probably wont be aware of -- best to have the syntax clear)
0
 

Author Closing Comment

by:jackie777
ID: 31578278
Thanks!
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now