Passing By Reference
When calling a method, or returning from it, data may be passed by value or by reference.
By reference, a reference to the data is passed: any modification on that value inside the method will be reflected in the original value.
This is the alternative way of passing data to methods: the default way is to use by value, which copies the data.
Passing by reference is the default behavior with object. Otherwise, it needs to be declared with the & operator, in the function signature.
Returned values are passed back to the calling context with the same rule as for parameters.
<?php
$trunk = 0;
trumpet($trunk); // displays 1
print $trunk; // displays 1
function trumpet(&$trunk) {
++$trunk;
echo $trunk;
}
?>