Pass by reference versus pass by value is a topic that a lot of people get mixed up about, but it’s pretty simple once you get the basic concepts down. First, lets take a look at the formal definition of pass by reference and pass by value (called call-by-reference and call-by-value on Wikipedia):
Call-by-value evaluation is the most common evaluation strategy, used in languages as different as C and Scheme. In call-by-value, the argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function (frequently by copying the value into a new memory region). If the function or procedure is able to assign values to its parameters, only its local copy is assigned — that is, anything passed into a function call is unchanged in the caller’s scope when the function returns. – Wikipedia
In call-by-reference evaluation (also referred to as pass-by-reference), a function receives an implicit reference to a variable used as argument, rather than a copy of its value. This typically means that the function can modify (i.e. assign to) the variable used as argument—something that will be seen by its caller. Call-by-reference can therefore be used to provide an additional channel of communication between the called function and the calling function. A call-by-reference language makes it more difficult for a programmer to track the effects of a function call, and may introduce subtle bugs.
Many languages support call-by-reference in some form or another, but comparatively few use it as a default, e.g. Perl. A few languages, such as C++, PHP, Visual Basic .NET, C# and REALbasic, default to call-by-value, but offer special syntax for call-by-reference parameters. C++ additionally offers call-by-reference-to-const. – Wikipedia
While some people may be able to understand what’s going on from the above definitions, they really aren’t that intuitive. Luckily a Stack Overflow answer provides a really simple and easy to understand example of the difference between pass by reference and pass by value. You can see it below:
Say I want to share a web page with you.
If I tell you the URL, I’m passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you’re doing is destroying your reference to that page – you’re not deleting the actual page itself.
If I print out the page and give you the printout, I’m passing by value. Your page is a disconnected copy of the original. You won’t see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object – but the original web page remains intact. – Stack Overflow
Since we usually use mostly PHP on this blog I’ve included an example of pass by reference versus pass by value below:
As you can see the pass by reference is denoted in the function definition by the
&$var which lets PHP know that that variable is being passed by reference (the actual variable from outside of the function will be changed). There is no reference sign on a function call – only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you would get a warning saying that “call-time pass-by-reference” is deprecated if you tried to do something like
pbr(&$num);. And as of PHP 5.4.0, “call-time pass-by-reference” was removed, so doing the above example will raise a fatal error.
It’s a little unfortunate that you can only denote pass by reference in the function definition because programmers calling your function might not know that it’s by reference. They might call the function and end up with unexpected results because they think they’re passing by value. For this reason, its important to make sure you think very carefully about passing by reference.
Hopefully this clears up some of the confusion some of you may have had. Pass by reference can be very useful for things such as sanitizing
$_POST variables, but just remember to think about who will be using your function because pass by reference isn’t denoted in function calls.
As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future tutorials in the comments section below.