Re: Arrays past to functions

From: Date: Fri, 27 Jul 2007 17:38:11 +0000
Subject: Re: Arrays past to functions
References: 1 2 3 4  Groups: php.windows 
Request: Send a blank email to php-windows+get-28267@lists.php.net to get a copy of this message
Ok, then let's start from the top: 1. foreach Foreach is a language construct used to loop over an entire array returning the (key and) value on each iteration. So: $a = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6); foreach($a as $key=>$value) { echo $key.' = '.$value; } will return a=1 b=2 etc. Basically it's almost the same as a for() loop but with less hassle and you don't need to know exactly how long it is. And one very important aspect here is that foreach works on both numeric indices AND non-numeric! Unlike for() which only works with numbers (in a "normal" setting). $a += $b; is short for $a = $a+$b; What I do here is I make the function recursive, ie. call itself from itself. A common example given is one to calculate faculty: function faculty($i) { if($i <= 1) {
      return 1;
} else {
       return $i*faculty($i-1);
} } faculty(3) returns: 6 because: faculty(3): if(3 <= 1) { // won't run } else { return 3*faculty(2); /* faculty(2) runs: return 2*faculty(1)
      faculty(1): return 1;
*/ } And the last, if you don't know what is_array() does, then please open a dictionary and lookup the word "is". So, what my example did was: 1. check if we were passed an array or not. 1.1 if it was an array, loop over the array and call this function on each element and add all returned values into 1 variable. 1.2 if it was NOT an array, compute the tax for that number and return it. You will notice that in PHP it is often much easier to use the foreach() construct than a for() loop because there's much less hassle. (like in your last post where you had problems with your for loop). Make sure to read the following pages from the manual: 1. Foreach construct: http://www.php.net/manual/en/control-structures.foreach.php 2. Operators: http://www.php.net/manual/en/language.operators.assignment.php 3. Arrays: http://www.php.net/manual/en/language.types.array.php Jacob Bergman wrote:
Thanks, unfortunately, I don't really know what the whole " if(is_array($State))
     {
     	$total = 0;
     	foreach($State as $s)
     	{
             $total += ComputeSalesTax($Amt, $s);
         }
         return $total;"
don't know enough yet to understand what all that means... I have gotten the function to work the way I want using a for loop in the function... but it doesn't seem to want to display the 0 keyed value in the array. If you run this, you'll see what I mean. Can you tell me what is wrong with it? ---------------------------------------------------------------------- $State = array ("WA" , "CA" , "OR"); function compute_salestax ($Amount , $State)
            {
             for ($i = 0 ; $i<= 2/*sizeof($State)*/ ; $i++)
                 {
                  switch ($State[$i])
                    {
                     case "WA" :
                          $Rate = "1.07";
                          break;
                     case "CA" :
                          $Rate = "1.08";
                          break;
                     case "OR" :
                          $Rate = "1.05";
                          break;
                    }
                    global $Rate;
                    $Truetax = ($Amount * $Rate);
                    echo "$i=";
                    echo "$Rate | $Truetax <br />";
                  }}
$Amount = 1850; compute_salestax($Amount , $State); --------------------------------------------------------------------- When I run it, I get this --------------------------------------------------------------------- 0= | 0 1=1.08 | 1998 2=1.05 | 1942.5 --------------------------------------------------------------------- Thanks for the help guys! Jacob Bergman Network Technician Pullman School District #267 (509) 432-4012 jbergman@psd267.wednet.edu -----Original Message----- From: M. Sokolewicz [mailto:tularis@php.net] Sent: Friday, July 27, 2007 9:34 AM To: Jacob Bergman Cc: Jarrett Meyer; php-windows@lists.php.net Subject: Re: [PHP-WIN] Arrays past to functions 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@psd267.wednet.edu -----Original Message----- From: Jarrett Meyer [mailto:jmtmeyer@yahoo.com] Sent: Friday, July 27, 2007 8:56 AM To: PHP Windows List Subject: Re: [PHP-WIN] 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@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


« previous php.windows (#28267) next »