Jump to content

funcion problem


charlie321

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.

 

 

Link to comment
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;
}
}

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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!

Link to comment
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.

Link to comment
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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.