Jump to content
charlie321

funcion problem

Recommended Posts

Trying to understand php functions.

function tax_invoice() {
if ($state == "FL") {
   $stax = ($subtotal-$credit+$shipping) * .07;
}
}

#call function:

tax_invoice();

I have tried different variations including in script, on own functions program, using return, adding variables, etc.  Nothing seems to work unless I use it as a non-function in code.

Please help.

 

 

Share this post


Link to post
Share on other sites

Variables defined outside of functions are not available inside of functions. You need to use function arguments to get values in, then return whatever single value you want out of it.

Share this post


Link to post
Share on other sites

I tried this which I thought might be what you were getting at but no success.  More help please.

function tax_invoice($subtotal,$credit,$shipping,$state) {
if ($state == "FL") {
   $stax = ($subtotal-$credit+$shipping) * .07;
   return $stax;
}
}

Share this post


Link to post
Share on other sites

What about states other than Florida?

Share this post


Link to post
Share on other sites

What exactly are you expecting and what exactly are you getting? You say you're trying to understand functions - groovy.

Functions basically work like this: you pass some parameters to a function when you call it (sometimes), and at the same time (most of the time) you assign whatever that function does with those parameters and then returns to a "local" variable. I put "local" in quotes because functions, methods, classes, and objects all have different meanings and potentially different scopes - which is another thing you'll want to look into as you learn the way this all works.

Share this post


Link to post
Share on other sites

I had taken the FL if statement out to see if that was the problem.  But did not work that way either.  This works fine in code.  Basically  am trying to get the result as sales tax for the state of Florida using subtotal of invoice, credit, and shipping totals.  Something within the function is preventing that from happening as it never gives anything but 0 for sales tax variable ($stax)  As I said it works fine in code.  I believe it is something to do with the variables not passing into or out of the function.  I have worked with functions in other languages with no problems passing variables. Has to be something I am missing.

Share this post


Link to post
Share on other sites

Post your modified function as well, as the code calling the function and any other code that could possibly be relevant to your problem.

Share this post


Link to post
Share on other sites

I have posted a modified function above already.  Please reference that.

Below is the code that woks fine as a non- function:

if ($state == "FL") {
   $stax = ($subtotal-$credit+$shipping) * .07;
  }

As I have said, this works but is redundant as it is presented several times throughout the code.  Thus my wanting to make it a function.

Share this post


Link to post
Share on other sites

You said you took the FL if statement out. You did not post that version. But it's not as important as posting the code that is calling this function.

Share this post


Link to post
Share on other sites

This is the code that is calling the function:

tax_invoice($state,$subtotal,$credit,$shipping);

I wrote an example that should show what is happening.  It does the exact same thing:

<?php
$stax = 0;
$subtotal = 100;
$ship = 15;
echo (($subtotal+$ship) * .07). "  This is outside the function  Correct answer.<p>";

function example($subtotal,$ship,$stax)  {

$stax == (($subtotal+$ship) * .07);
echo $stax . "  This is inside the function. Wrong answer.<p>";
return $stax;
}

example($subtotal,$ship,$stax);  ##calling function.  Nothing there.

echo " This is calling the function.  Gives a null which is wrong.";
?>

There is something I'm not getting.  I would appreciate someone who knows php functions show me what I am doing wrong in the little script that describes the problem.

I'd really appreciate it!

Share this post


Link to post
Share on other sites

Yes that was the problem with the script which I finally figured out just after my last post.  And then I realized this would work better.   $stax = tax_invoice($state,$subtotal,$credit,$shipping);   Blood dribbling down my head from the wall punches.   Take care.

Share this post


Link to post
Share on other sites

 

Quote

This is the final script:

function tax_invoice($state,$subtotal,$credit,$shipping) {
   if ($state == "FL") {
     $stax = ($subtotal-$credit+$shipping) * .07;
        } else {
     $stax = 0;
}
return $stax;
}

I found php functions to be confusing, but at least I finally got it done.  Thanks for the suggestions which helped get me on the right track.

 

Share this post


Link to post
Share on other sites

The else is not needed.

 

function tax_invoice($state, $subtotal, $credit, $shipping) {
    if ($state == "FL") {
        return ($subtotal - $credit + $shipping) * .07;
    }
    return 0;
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.