Solved

PHP calculate tax function

Posted on 2009-05-05
6
978 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

737 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