Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP calculate tax function

Posted on 2009-05-05
6
Medium Priority
?
1,005 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

636 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