Sara,
You're on the right track.  You've created your functions, but they need
some work before they can be used.

Starting with getTax()
you've got a naming convention conflict, while not necessarily a problem,
it's bad style. Instead of $item as the parameter for the function, use
something like $price, so:
function getTax($price)
{
        $tax = $price * 0.065;
        return $tax;
}

or even better:
function getTax($price)
{
        return $price * 0.065;
}
(notice I removed the echo statement)

For the second function getBalance(), you've got a scope problem.
Looks like you're trying to use the value that $tax contains.  But $tax only
has that value within the getTax function.

If your getBalance() function was this:
function getBalance($item)
{
        echo $tax;
}
you'd get nothing (well you'd get errors/warnings), because $tax isn't
defined within the scope of getBalance().  If you want to use $tax, then you
have to pass it as an attribute of the function like this:
function getBalance($price,$tax)
{
        echo $tax;
}

Another problem, similar to the $tax, is your use of $balance.  It will have
no value, until you add the subtotal, and then 1 to it, then you'll have the
subtotal +1, i.e. numbers like 15976, 2.065.
So in addition to passing the tax you'll have to pass the current balance.

Here's the new improved getBalance() function:
function getBalance($price, $tax, $balance)
{
        $subtotal = $price + $tax;
        $balance = $balance + $subtotal + 1;
        return $balance;
}

I've no idea why you're adding 1, but what the heck.

Now that you've got improved functions, you need to call them.  To do that
you need to do something like:

$tax = getTax($price);

Then to use the 'new' second function try:

$balance = getBalance($price,$tax,$balance);

Hope this is helpful.
Chris


-----Original Message-----
From: Sara Daugherty [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 19, 2003 11:54 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: [PHP] tax functions


Hello,
I am new to php. I am trying to learn functions.
Can someone look at my code and tell me if I am on the right track or if
I am doing something wrong?
Here is the link where my script can be found:
http://www.areawebdirectory.net/taxfunction.txt

Thanks,
Sara D.

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

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

Reply via email to