Re: Arrays past to functions
| From: | Jarrett Meyer | Date: | Fri, 27 Jul 2007 15:55:36 +0000 |
| Subject: | Re: Arrays past to functions | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-28260@lists.php.net to get a copy of this message | ||
You don't really need an array here.
function ComputeSalesTax($Amt, $State)
{
switch ($State)
{
case "CA":
$tax = 1.05;
break;
case "WA":
$tax = 1.07;
break;
// etc.
default:
$tax = false;
}
if ($tax != false)
{
return ($Amt * $tax);
}
else
{
die("State not found!");
// or some other graceful method...
}
}
ComputeSalesTax(2000, "CA");
// returns 2100
ComputeSalesTax(2000, "LA");
// fails
--
Jarrett M. T. Meyer
http://jarrettmeyer.blogspot.com
http://www.jarrettmeyer.com
----- Original Message ----
From: Jacob Bergman <jbergman@psd267.wednet.edu>
To: php-windows@lists.php.net
Sent: Friday, July 27, 2007 11:46:19 AM
Subject: [PHP-WIN] Arrays past to functions
Hello all,
I am learning about passing arrays to functions, and am trying to
create a function that will display a total amount for a price for
several states. Say using CA, WA, and OR, and setting there tax rates
at, whatever, say 5%, 7%, and 8% respectively. I want a function I can
pass the array of states, and tax rates to that will display the total
amount (amount + tax) of say a $1500 purchase. I am trying to use a
switch statement to accomplish this, but I think I may be in over my
head trying to do this with what I have learned so far... Here is the
code I have so far I get 0 every time I run it, try not to laugh... :-)
Thanks guys.
$State = array ("CA" , "WA" , "OR");
function compute_salestax ($Amount , $State)
{
switch ($State)
{
case "CA" :
$Rate = "1.05";
break;
case "WA" :
$Rate = "1.07";
break;
case "OR" :
$Rate = "1.08";
break;
}
global $Rate;
$Truetax = ($Amount * $Rate);
echo "$Truetax <br />";
}
$Amount = 1500;
compute_salestax($Amount , $State);
Jacob Bergman
Network Technician
Pullman School District #267
(509) 432-4012
jbergman@psd267.wednet.edu