Jump to content

How does this function work? A basic php question


Recommended Posts

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

 

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

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.

 

 

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.

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.