[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP calculate tax function

Posted on 2009-05-05
6
Medium Priority
?
1,015 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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