Arrays past to functions

Arrays past to functions

am 27.07.2007 17:46:19 von Jacob Bergman

------_=_NextPart_001_01C7D065.46BE41A0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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.

=20

=20

$State =3D array ("CA" , "WA" , "OR");

function compute_salestax ($Amount , $State)

{

switch ($State)

{

case "CA" :

$Rate =3D "1.05";

break;

case "WA" :

$Rate =3D "1.07";

break;

case "OR" :

$Rate =3D "1.08";

break;

}

=20

global $Rate;

$Truetax =3D ($Amount * $Rate);

echo "$Truetax
";

}

$Amount =3D 1500;

compute_salestax($Amount , $State);

=20

=20

=20

Jacob Bergman

Network Technician

Pullman School District #267

(509) 432-4012

jbergman@psd267.wednet.edu

=20


------_=_NextPart_001_01C7D065.46BE41A0--

Re: Arrays past to functions

am 27.07.2007 17:55:36 von Jarrett Meyer

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
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
";

}

$Amount = 1500;

compute_salestax($Amount , $State);







Jacob Bergman

Network Technician

Pullman School District #267

(509) 432-4012

jbergman@psd267.wednet.edu



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

RE: Arrays past to functions

am 27.07.2007 18:01:11 von Jacob Bergman

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]=20
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 =3D 1.05;
break;
case "WA":
$tax =3D 1.07;
break;
// etc.
default:
$tax =3D false;
}
if ($tax !=3D false)
{
return ($Amt * $tax);
}
else
{
die("State not found!");
// or some other graceful method...
}
}

ComputeSalesTax(2000, "CA");
// returns 2100

ComputeSalesTax(2000, "LA");
// fails
=20
--
Jarrett M. T. Meyer
http://jarrettmeyer.blogspot.com
http://www.jarrettmeyer.com

----- Original Message ----
From: Jacob Bergman
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.

=20

=20

$State =3D array ("CA" , "WA" , "OR");

function compute_salestax ($Amount , $State)

{

switch ($State)

{

case "CA" :

$Rate =3D "1.05";

break;

case "WA" :

$Rate =3D "1.07";

break;

case "OR" :

$Rate =3D "1.08";

break;

}

=20

global $Rate;

$Truetax =3D ($Amount * $Rate);

echo "$Truetax
";

}

$Amount =3D 1500;

compute_salestax($Amount , $State);

=20

=20

=20

Jacob Bergman

Network Technician

Pullman School District #267

(509) 432-4012

jbergman@psd267.wednet.edu

=20

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

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

Re: Arrays past to functions

am 27.07.2007 18:33:36 von Tularis

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
> 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
";
>
> }
>
> $Amount = 1500;
>
> compute_salestax($Amount , $State);
>
>
>
>
>
>
>
> Jacob Bergman
>
> Network Technician
>
> Pullman School District #267
>
> (509) 432-4012
>
> jbergman@psd267.wednet.edu
>
>
>

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

RE: Arrays past to functions

am 27.07.2007 18:52:30 von Jacob Bergman

Thanks, unfortunately, I don't really know what the whole

" if(is_array($State))
{
$total =3D 0;
foreach($State as $s)
{
$total +=3D 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 =3D array ("WA" , "CA" , "OR");
function compute_salestax ($Amount , $State)
{
for ($i =3D 0 ; $i<=3D 2/*sizeof($State)*/ ; $i++)
{
switch ($State[$i])
{
case "WA" :
$Rate =3D "1.07";
break;
case "CA" :
$Rate =3D "1.08";
break;
case "OR" :
$Rate =3D "1.05";
break;
}
global $Rate;
$Truetax =3D ($Amount * $Rate);
echo "$i=3D";
echo "$Rate | $Truetax
";
}}

$Amount =3D 1850;

compute_salestax($Amount , $State);

------------------------------------------------------------ ---------
When I run it, I get this
------------------------------------------------------------ ---------
0=3D | 0=20
1=3D1.08 | 1998=20
2=3D1.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]=20
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 =3D 0;
foreach($State as $s)
{
$total +=3D ComputeSalesTax($Amt, $s);
}
return $total;
}
else {
switch ($State)
{
case "CA":
$tax =3D 1.05;
break;
case "WA":
$tax =3D 1.07;
break;
// etc.
default:
$tax =3D false;
}
if ($tax !=3D 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.
>=20
> Jacob Bergman
> Network Technician
> Pullman School District #267
> (509) 432-4012
> jbergman@psd267.wednet.edu
>=20
> -----Original Message-----
> From: Jarrett Meyer [mailto:jmtmeyer@yahoo.com]=20
> Sent: Friday, July 27, 2007 8:56 AM
> To: PHP Windows List
> Subject: Re: [PHP-WIN] Arrays past to functions
>=20
> You don't really need an array here.
>=20
> function ComputeSalesTax($Amt, $State)
> {
> switch ($State)
> {
> case "CA":
> $tax =3D 1.05;
> break;
> case "WA":
> $tax =3D 1.07;
> break;
> // etc.
> default:
> $tax =3D false;
> }
> if ($tax !=3D false)
> {
> return ($Amt * $tax);
> }
> else
> {
> die("State not found!");
> // or some other graceful method...
> }
> }
>=20
> ComputeSalesTax(2000, "CA");
> // returns 2100
>=20
> ComputeSalesTax(2000, "LA");
> // fails
> =20
> --
> Jarrett M. T. Meyer
> http://jarrettmeyer.blogspot.com
> http://www.jarrettmeyer.com
>=20
> ----- Original Message ----
> From: Jacob Bergman
> To: php-windows@lists.php.net
> Sent: Friday, July 27, 2007 11:46:19 AM
> Subject: [PHP-WIN] Arrays past to functions
>=20
> Hello all,
>=20
> 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.
>=20
> =20
>=20
> =20
>=20
> $State =3D array ("CA" , "WA" , "OR");
>=20
> function compute_salestax ($Amount , $State)
>=20
> {
>=20
> switch ($State)
>=20
> {
>=20
> case "CA" :
>=20
> $Rate =3D "1.05";
>=20
> break;
>=20
> case "WA" :
>=20
> $Rate =3D "1.07";
>=20
> break;
>=20
> case "OR" :
>=20
> $Rate =3D "1.08";
>=20
> break;
>=20
> }
>=20
> =20
>=20
> global $Rate;
>=20
> $Truetax =3D ($Amount * $Rate);
>=20
> echo "$Truetax
";
>=20
> }
>=20
> $Amount =3D 1500;
>=20
> compute_salestax($Amount , $State);
>=20
> =20
>=20
> =20
>=20
> =20
>=20
> Jacob Bergman
>=20
> Network Technician
>=20
> Pullman School District #267
>=20
> (509) 432-4012
>=20
> jbergman@psd267.wednet.edu
>=20
> =20
>=20

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

Re: Arrays past to functions

am 27.07.2007 18:59:02 von Niel Archer

Hi Jacob

try this:

function compute_salestax ($Amount , $State)
{
$taxRate = array("CA" => 5 , "WA" => 7, "OR" => 8);
return $Amount * $taxRate($state);
}

$payment = 1500;
$locale = 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment , $locale);

--
Niel Archer

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

RE: Arrays past to functions

am 27.07.2007 19:35:29 von Jacob Bergman

I tried using that, but all I get is these errors:

Notice: Undefined variable: amount in C:\xampp\htdocs\test3.php on line
16

Fatal error: Function name must be a string in C:\xampp\htdocs\test3.php
on line 11

Jacob Bergman
Network Technician
Pullman School District #267
(509) 432-4012
jbergman@psd267.wednet.edu

-----Original Message-----
From: Niel Archer [mailto:spam-free@blueyonder.co.uk]=20
Sent: Friday, July 27, 2007 9:59 AM
To: php-windows@lists.php.net
Subject: Re: [PHP-WIN] Arrays past to functions

Hi Jacob

try this:

function compute_salestax ($Amount , $State)
{
$taxRate =3D array("CA" =3D> 5 , "WA" =3D> 7, "OR" =3D> 8);
return $Amount * $taxRate($state);
}

$payment =3D 1500;
$locale =3D 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment ,
$locale);

--
Niel Archer

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

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

Re: Arrays past to functions

am 27.07.2007 19:38:11 von Tularis

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.p hp
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
";
> }}
>
> $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
>> 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
";
>>
>> }
>>
>> $Amount = 1500;
>>
>> compute_salestax($Amount , $State);
>>
>>
>>
>>
>>
>>
>>
>> Jacob Bergman
>>
>> Network Technician
>>
>> Pullman School District #267
>>
>> (509) 432-4012
>>
>> jbergman@psd267.wednet.edu
>>
>>
>>

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

Re: Arrays past to functions

am 27.07.2007 20:03:00 von Niel Archer

Hi Jacob

Sorry, my fault entirely. I wrote it in a hurry after copy/pasting your
example. I didn't change the variable names properly or test it.

I still haven't been able to test, but this should work better now.

function compute_salestax ($Amount , $State)
{
$taxRate = array("CA" => 5 , "WA" => 7, "OR" => 8);
return $Amount * $taxRate($State);
}

$payment = 1500;
$locale = 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment , $locale);


--
Niel Archer

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

RE: Arrays past to functions

am 27.07.2007 20:07:08 von Jacob Bergman

Thanks a bunch guys, I'm out for the day, I will pick this back up on
Monday, have a great weekend.

Jacob Bergman
Network Technician
Pullman School District #267
(509) 432-4012
jbergman@psd267.wednet.edu

-----Original Message-----
From: Niel Archer [mailto:spam-free@blueyonder.co.uk]=20
Sent: Friday, July 27, 2007 11:03 AM
To: php-windows@lists.php.net
Subject: Re: [PHP-WIN] Arrays past to functions

Hi Jacob

Sorry, my fault entirely. I wrote it in a hurry after copy/pasting your
example. I didn't change the variable names properly or test it.

I still haven't been able to test, but this should work better now.

function compute_salestax ($Amount , $State)
{
$taxRate =3D array("CA" =3D> 5 , "WA" =3D> 7, "OR" =3D> 8);
return $Amount * $taxRate($State);
}

$payment =3D 1500;
$locale =3D 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment ,
$locale);


--
Niel Archer

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

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

Re: Arrays past to functions

am 27.07.2007 20:40:02 von Niel Archer

Hi Jacob.

Spotted another problem.

function compute_salestax ($Amount , $State)
{
$taxRate = array("CA" => 5 , "WA" => 7, "OR" => 8);
return $Amount / 100 * $taxRate($State);
}

$payment = 1500;
$locale = 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment , $locale);

--
Niel Archer

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

Re: Arrays past to functions

am 27.07.2007 20:49:54 von Tularis

Niel Archer wrote:
> Hi Jacob.
>
> Spotted another problem.
>
> function compute_salestax ($Amount , $State)
> {
> $taxRate = array("CA" => 5 , "WA" => 7, "OR" => 8);
> return $Amount / 100 * $taxRate($State);
> }
>
> $payment = 1500;
> $locale = 'CA';
> print "Tax on $amount in $locale is " . compute_salestax ($payment , $locale);
>
> --
> Niel Archer

hi Niel,

I believe Jacob wanted to know how he could calculate the tax for _all_
states passed in as an array in one go. Yours simply gives it for 1
State ("locale"). Other than thatm $taxRate($state) translates to
Array($state) which gives an E_FATAL_ERROR obviously. I assume you meant
to say $taxRate[$State].

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

Re: Arrays past to functions

am 27.07.2007 21:40:02 von Niel Archer

Hi

> hi Niel,
>
> I believe Jacob wanted to know how he could calculate the tax for _all_
> states passed in as an array in one go. Yours simply gives it for 1
> State ("locale"). Other than thatm $taxRate($state) translates to
> Array($state) which gives an E_FATAL_ERROR obviously. I assume you meant
> to say $taxRate[$State].

After re-reading Jacob's post I have to agree I misunderstand. Here's a
better answer to his request.

// Create array of locales we want tax for
$locales = array("CA", "OR");
$payment = 1500;

ShowSalesTax($payment, $locales);

function ShowSalesTax($Amount , $States)
{
//aray of states and their rates
$StatesRates = array("CA" => 5 , "WA" => 7, "OR" => 8);
foreach($States as $state):
print "Tax on $Amount in $state is " . $payment / 100 * $StatesRates[$state] . "\n";
endforeach;
}

Obviously, it's only an example and needs refining.

--
Niel Archer

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

Re: Arrays past to functions

am 28.07.2007 00:57:56 von Tularis

Niel Archer wrote:
> Hi
>
>> hi Niel,
>>
>> I believe Jacob wanted to know how he could calculate the tax for _all_
>> states passed in as an array in one go. Yours simply gives it for 1
>> State ("locale"). Other than thatm $taxRate($state) translates to
>> Array($state) which gives an E_FATAL_ERROR obviously. I assume you meant
>> to say $taxRate[$State].
>
> After re-reading Jacob's post I have to agree I misunderstand. Here's a
> better answer to his request.
>
> // Create array of locales we want tax for
> $locales = array("CA", "OR");
> $payment = 1500;
>
> ShowSalesTax($payment, $locales);
>
> function ShowSalesTax($Amount , $States)
> {
> //aray of states and their rates
> $StatesRates = array("CA" => 5 , "WA" => 7, "OR" => 8);
> foreach($States as $state):
> print "Tax on $Amount in $state is " . $payment / 100 * $StatesRates[$state] . "\n";
> endforeach;
> }
>
> Obviously, it's only an example and needs refining.
>
> --
> Niel Archer


I must say, you're the first (and only) person I've ever seen using
foreach(): endforeach in PHP. As far as I know, most people use
curly-bracers (in this case), I'm sure most people wouldn't even know
this was possible (I had to look it up in the manual, and it's only in a
single note that this is mentioned)

- Tul

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

Re: Arrays past to functions

am 28.07.2007 01:10:42 von Niel Archer

> I must say, you're the first (and only) person I've ever seen using
> foreach(): endforeach in PHP. As far as I know, most people use
> curly-bracers (in this case), I'm sure most people wouldn't even know
> this was possible (I had to look it up in the manual, and it's only in a
> single note that this is mentioned)

Indeed, it's a modern thing and one habit I'm trying to get out of.
--
Niel Archer

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

RE: Arrays past to functions

am 28.07.2007 15:27:48 von Jeff White

Hello Jacob,

This may be what you're looking for:


function compute_salestax ($Amount, $Rates = array())
{
foreach($Rates as $State => $Rate)
{
$Ret[$State] = $Amount * ($Rate/100);
}
return $Ret;
}

echo "Payment: " . $payment = 1500;
$taxRates = array("CA" => 5 , "WA" => 7, "OR" => 8);
$taxresults = compute_salestax ($payment, $taxRates);
foreach($taxresults as $state => $value)
{
print "
Tax = " . sprintf("%01.2f", round($value, 2)) . " in $state.";
}

?>

Browser output results:

Payment: 1500
Tax = 75.00 in CA.
Tax = 105.00 in WA.
Tax = 120.00 in OR.

I hope this helps!!

Jeff White





-----Original Message-----
From: Jacob Bergman [mailto:jbergman@psd267.wednet.edu]
Sent: Friday, July 27, 2007 2:07 PM
To: php-windows@lists.php.net
Subject: RE: [PHP-WIN] Arrays past to functions

Thanks a bunch guys, I'm out for the day, I will pick this back up on
Monday, have a great weekend.

Jacob Bergman
Network Technician
Pullman School District #267
(509) 432-4012
jbergman@psd267.wednet.edu

-----Original Message-----
From: Niel Archer [mailto:spam-free@blueyonder.co.uk]
Sent: Friday, July 27, 2007 11:03 AM
To: php-windows@lists.php.net
Subject: Re: [PHP-WIN] Arrays past to functions

Hi Jacob

Sorry, my fault entirely. I wrote it in a hurry after copy/pasting your
example. I didn't change the variable names properly or test it.

I still haven't been able to test, but this should work better now.

function compute_salestax ($Amount , $State)
{
$taxRate = array("CA" => 5 , "WA" => 7, "OR" => 8);
return $Amount * $taxRate($State);
}

$payment = 1500;
$locale = 'CA';
print "Tax on $amount in $locale is " . compute_salestax ($payment ,
$locale);


--
Niel Archer

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

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

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

Re: Arrays past to functions

am 31.07.2007 11:37:06 von Abhisek Dutta

Here's what i made:
$taxrate=array('CA'=>5,'WA'=>7,'OR'=>8);
$states=array('CA','WA','OR');
function calctax($amount, $st)
{
global $taxrate;
global $states;
$tax=$amount*$taxrate[$st];
return $tax/100;
}
$amnt=200;
foreach($states as $st)
{
print "Tax in $st for amount $amnt is ".calctax($amnt, $st);
print "
\n";
}
?>
Output:
Tax in CA for amount 200 is 10
Tax in WA for amount 200 is 14
Tax in OR for amount 200 is 16/
/

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

Re: Arrays past to functions

am 31.07.2007 11:44:29 von vikas batra

--0-2106117022-1185875069=:41520
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Wat is the problem with this code.=0A =0A----- Original Message ----=
=0AFrom: Abhisek Dutta =0ATo: php-windows@lists.php=
..net=0ASent: Tuesday, 31 July, 2007 3:07:06 PM=0ASubject: Re: [PHP-WIN] Arr=
ays past to functions Here's what i made:=0A =3D>5,'WA'=3D>7,'OR'=3D>8);=0A$states=3Darray('CA','WA','OR' );=0Afunction c=
alctax($amount, $st)=0A{=0Aglobal $taxrate;=0Aglobal $states;=0A$tax=3D$amo=
unt*$taxrate[$st];=0Areturn $tax/100;=0A}=0A$amnt=3D200;=0Aforeach($states =
as $st)=0A{=0Aprint "Tax in $st for amount $amnt is ".calctax($amnt, $st);=
=0Aprint "
\n";=0A}=0A?>=0AOutput:=0ATax in CA for amount 200 is 10=0AT=
ax in WA for amount 200 is 14=0ATax in OR for amount 200 is 16/=0A/ --=
=0APHP Windows Mailing List (http://www.php.net/)=0ATo unsubscribe, visit:=
http://www.php.net/unsub.php =0A Bollywood, fu=
n, friendship, sports and more. You name it, we have it on http://in.groups=
..yahoo.com
--0-2106117022-1185875069=:41520--