charlie321 Posted June 6, 2020 Share Posted June 6, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/ Share on other sites More sharing options...
requinix Posted June 6, 2020 Share Posted June 6, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578691 Share on other sites More sharing options...
charlie321 Posted June 7, 2020 Author Share Posted June 7, 2020 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578692 Share on other sites More sharing options...
requinix Posted June 7, 2020 Share Posted June 7, 2020 What about states other than Florida? Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578693 Share on other sites More sharing options...
maxxd Posted June 7, 2020 Share Posted June 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578694 Share on other sites More sharing options...
charlie321 Posted June 7, 2020 Author Share Posted June 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578695 Share on other sites More sharing options...
requinix Posted June 7, 2020 Share Posted June 7, 2020 Post your modified function as well, as the code calling the function and any other code that could possibly be relevant to your problem. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578696 Share on other sites More sharing options...
charlie321 Posted June 7, 2020 Author Share Posted June 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578698 Share on other sites More sharing options...
requinix Posted June 7, 2020 Share Posted June 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578699 Share on other sites More sharing options...
charlie321 Posted June 7, 2020 Author Share Posted June 7, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578701 Share on other sites More sharing options...
benanamen Posted June 7, 2020 Share Posted June 7, 2020 You do realize there is a difference between == and = right? Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578702 Share on other sites More sharing options...
charlie321 Posted June 7, 2020 Author Share Posted June 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578703 Share on other sites More sharing options...
charlie321 Posted June 9, 2020 Author Share Posted June 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578742 Share on other sites More sharing options...
benanamen Posted June 9, 2020 Share Posted June 9, 2020 The else is not needed. function tax_invoice($state, $subtotal, $credit, $shipping) { if ($state == "FL") { return ($subtotal - $credit + $shipping) * .07; } return 0; } Quote Link to comment https://forums.phpfreaks.com/topic/310900-funcion-problem/#findComment-1578764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.