?
Solved

PHP calculate tax function

Posted on 2009-05-05
6
Medium Priority
?
992 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Industry Leaders: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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 …
Suggested Courses

800 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