Dec

13

There are two different ways to pass parameters to a function. The first is by value; the other is by reference.

Passing Parameters by Value
Passing parameters by value is the common way to pass parameters, below is an example

function addFirstNameTo($value) {
$value = "Franz ".$value;
echo $value;
}
$name = "Ferdinand";
addFirstNameTo($name);

The output will be: Franz Ferdinand. Because the function’s $value parameter is passed by value, a copy of value of $name , is modified by the function. The actual value of $name is unaffected, thus remains the same.

Passing Parameters by Reference
Passing by reference allows you to override the normal scoping rules and give a function direct access to a variable. Let’s rewrite the code above, so that variable $name will be changed from a glabal scope:

function addFirstName(&$value) {
$value = "Franz ".$value;
}
$name = "Ferdinand";
addFirstName($name);
echo $name;

By executing the code above, you will get the same output: Franz Ferdinand. But the concept is profoundly different from the first example. Because the function’s $value parameter is passed by reference, the actual value of $name, rather than a copy of $name’s value, is modified by the function. &$value is nothing but a reference to any variable the function is assigned to.

A parameter that is declared as being passed by reference can only be a variable. Thus, if we included the statement in the previous example, it would issue an error. However, you may assign a default value to parameters passed by reference. Below is an example of assigning a default value to parameters passed by reference.

function addFirstNameTo(&$value="Ferdinand") {
echo "Now we have Franz ".$value."\n";
}
$Val = "Lizst";
addFirstNameTo();
addFirstNameTo($Val);

In this example, Ferdinand is the default value. The output of the above code would be:

Now we have Franz Ferdinand
Now we have Franz Lizst

Now, now let’s compare ‘Passing Parameters by Value’ with ‘Passing Parameters by Reference’. I think ‘Passing Parameters by Reference’ is a better way in most cases. Even in cases where your function does not affect the given value, you may want a parameter to be passed by reference. When passing by value, PHP must copy the value. Particularly for large strings and objects, this can be an expensive operation. Passing by reference removes the need to copy the value. I found it really powerful after using it for a while.



Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

1 Comment so far

  1. dan » PHP Function Parameters - Value vs Reference on December 16, 2007 3:20 am

    [...] Check it out! While looking through the blogosphere we stumbled on an interesting post today.Here’s a quick excerptPassing by reference allows you to override the normal scoping rules and give a function direct access to a variable. Let’s rewrite the code above, so that variable $name will be changed from a glabal scope: … [...]

Sponsors




Links