bcb Posted October 12, 2010 Share Posted October 12, 2010 Hi, Im learning php, but Im a bit confused on this basic problem. I have the following code: <?php include "tax.php"; $price = 95; echo "Price before tax: $price <br>"; echo "Price after tax: "; echo add_tax ($price); ?> Which uses the function below <?php function add_tax ($amount) { $total = $amount * 1.09; return $total; } ?> When I execute the code I can see that it does work, but I do not understand why this part works! echo add_tax ($price); There is no multiplier symbol here, so is the function performed on the ($price) variable simply because of where it is located on this line of code? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/215715-how-does-this-function-work-a-basic-php-question/ Share on other sites More sharing options...
MatthewJ Posted October 12, 2010 Share Posted October 12, 2010 Your $price variable is being "passed" to the function as an argument. So basically, when you call the function you give it the price to add tax to. Another example would be something like function test($var1, $var2){return $var1." ".$var2;} That is a simple function that would concatenate two strings together with a space in the middle... so this function is looking for two arguments and would be called like echo test("Hello", "world"); Which would echo: Hello world HTH Quote Link to comment https://forums.phpfreaks.com/topic/215715-how-does-this-function-work-a-basic-php-question/#findComment-1121532 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 The real source to this is return When you return something within a function, you can call that function as if it were a variable. If your add_tax function had just simply multiplied your argument and exited, you wouldn't have got anything; an error at the least. Using MathewJ's example..as an example, you can see that when test is called it will return a string with the two arguments concatenated echo test("this and", "that"); would yield: this and that. Quote Link to comment https://forums.phpfreaks.com/topic/215715-how-does-this-function-work-a-basic-php-question/#findComment-1121643 Share on other sites More sharing options...
atrum Posted October 13, 2010 Share Posted October 13, 2010 This is about the most simple way I can think to explain this. <?php function add_tax ($amount) { $total = $amount * 1.09; return $total; } ?> Given the example you provided above. $total = $amount * 1.09 Lets say the $amount = 7 $total = 7 * 1.09 $total now = 7.63 return $total return when used in a function like the above poster stated makes the function = what was returned. So now add_tax (7) is equal to 7.63 Here is a another way you could use that function. $num = add_tax(7); $num2 = add_tax(2); $total = $num * $num2; echo add_tax($total); The above is just a useless example, but it illustrates a bit more on how it works. Quote Link to comment https://forums.phpfreaks.com/topic/215715-how-does-this-function-work-a-basic-php-question/#findComment-1121651 Share on other sites More sharing options...
bcb Posted October 13, 2010 Author Share Posted October 13, 2010 Thank you all for your help! Now I understand how it works and can continue into the depths of learning. You all really know your stuff! Quote Link to comment https://forums.phpfreaks.com/topic/215715-how-does-this-function-work-a-basic-php-question/#findComment-1121722 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.