PHP Tutorial: Passing Data
The usefulness of functions is greatly enhanced by the ability to pass information from the main body of code to the function and in the opposite direction, or between one function and another it is calling Values that are sent to a function are called arguments.
Passing Argument to a Function
To define arguments that will be passed to a function, you provide a list of names for them between the parentheses in the function statement, like this:
function Myfunction ($arg1, $arg2)
and to pass values to the function as arguments by putting them between the parentheses following the function's name in the statement that calls it, like this:
$answer = Myfunction (3, 5)
I've used actual number here, but I could also have used variables. There is something to watch out for with those variables! Remember from the discussion about variables that variables have a scope? This becomes very important here. The function statement that defines a function is part of the function it defines; therefore variable defined in that same statement are also part of the function being defined; therefore their scope is local to the function. This means that the variable's lifetime begins when the function starts to be processed and ends when the function ends. Consequently, these variables are not available in the main body of code (or in the calling function, if this function is being called from within another function.) Simply using the same name won't make it available! In fact, using the same name in the main body of code will mean that you have two different variables with the same name. This will not create an error, but it will certainly create a confusing program to work on! It is best to use different names for the variables within your functions (another "be your own best friend" tip!)
Returning Results from a Function
A function returns a result to the calling code by using the "return: keyword, like this:
When the return statement is processed, the function terminates and the value of the variable specified is returned to the calling code. The return statement only returns one variable -- if you need to return more than one value, you'd have to define and return an array (we'll be getting into arrays a little later in this series.)
You could have more than one return statement in your function if, for example, you had conditional logic (such as "if" statements) within your function, but only one of them will ever be processed (since processing it terminates the function!)
Here's an example of a couple of variables being passed to a function as arguments with a result returned:
<title>Function Arguments Example</title>
<h1>Showing a Volume</h1>
function CalcVol ($x, $y, $z)
$vol = $x * $y * $z;
$long = 3;
$wide = 4;
$high = 5;
print "The volume of this object is " . CalcVol($long, $wide, $high) . " volumetric units.";
In this example you can see that a function called CalcVol is being defined, and that it receives three parameters, $x, $y & $z. These are being used to specify the lengths of the three axes of a solid object, which the function multiplies together to determine the volume of the object. The function then returns the volume to the calling code.
The function is being called from the middle of a print statement, which passes the three values contained in the variables $long, $wide & $high. Thus the print statement will display the result of the calculation.
In the next part of this tutorial series, we'll take a look and some functions that are built in to PHP and at some more sophisticated ways to manipulate data being passed back and forth.