Re: Arrays past to functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



function ComputeSalesTax($Amt, $State)
{
    if(is_array($State))
    {
    	$total = 0;
    	foreach($State as $s)
    	{
            $total += ComputeSalesTax($Amt, $s);
        }
        return $total;
    }
    else {
	    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...
	    }
    }
}

Jacob Bergman wrote:
Your right, and I have gotten it to work that way, but I wanted to see
how it would be done if I was to use an array.  I was trying to do
something easy just so I could "see how it would be done" I guess you
could say.  Plus when I run the function, I want it to output the total
price for all states in the array, and not have to call the function and
pass it a state and amount for each state.  Thanks for the reply.

Jacob Bergman
Network Technician
Pullman School District #267
(509) 432-4012
jbergman@xxxxxxxxxxxxxxxxx

-----Original Message-----
From: Jarrett Meyer [mailto:jmtmeyer@xxxxxxxxx] Sent: Friday, July 27, 2007 8:56 AM
To: PHP Windows List
Subject: Re:  Arrays past to functions

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@xxxxxxxxxxxxxxxxx>
To: php-windows@xxxxxxxxxxxxx
Sent: Friday, July 27, 2007 11:46:19 AM
Subject:  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@xxxxxxxxxxxxxxxxx


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux