MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Pass by Reference


This allows you to pass a variable by reference to a function or element that allows you to modify the original variable.
Passing-by-reference is not limited to variables only, the following can also be passed by reference:

New statements, e.g. foo(new SomeClass)
References returned from functions


Arrays


A common use of "passing-by-reference" is to modify initial values within an array without going to the extent of creating new arrays or littering your namespace. Passing-by-reference is as simple as preceding/prefixing the variable with an & => &$myElement.
Below is an example of harnessing an element from an array and simply adding 1 to its initial value.


$arr = array(1, 2, 3, 4, 5);
foreach($arr as &$num) {
 $num++;
}

Now when you harness any element within $arr, the original element will be updated as the reference was increased. You can verify this by:


print_r($arr);

You should take note when harnessing pass by reference within loops. At the end of the above loop, $num still holds a reference to the last element of the array. Assigning it post loop will end up manipulating the last array element! You can ensure this doesn't happen by unset()'ing it post-loop:


$myArray = array(1, 2, 3, 4, 5);
foreach($myArray as &$num) {
 $num++;
}
unset($num);

Functions


Another common usage for passing-by-reference is within functions. Modifying the original variable is as simple as:


$var = 5;
// define
function add(&$var) {
 $var++;
}
// call
add($var);

Which can be verified by echo'ing the original variable.


echo $var;

There are various restrictions around functions, as noted below from the PHP docs:

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 will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.



Conclusion

In this page (written and validated by ) you learned about PHP Pass by Reference . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Arrays.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.