Jump to content

passing by reference


jimfog

Recommended Posts

In the php manual it is mentioned that the passing by reference is used so that a function can alter the value of the variable.The following example is taken by the PHP manual:

<?phpfunction foo(&$var){    $var++;}$a=5;foo($a);// $a is 6 here?>

What I want to say I can get the result-namely 6-without using the passing by reference method.DO I miss something here?

Link to comment
Share on other sites

are you asking if there's another way to write that same function without passing by reference, that is something other than using a return statement?

Link to comment
Share on other sites

They're just showing how passing by reference works. If you didn't have the & operator then that code would not work properly.
What I am saying is that the result is the same even if I remove the reference operator.Here is the code again but with an echo statement just to make tests:
function foo(&$var){    $var++;echo $var;}$a=4;foo($a);

The result is 5 in both cases-with or without the reference operator.

Link to comment
Share on other sites

You're missing the point of a reference - with a reference, the value can be changed so that code outside the function can see a value that was set inside the function.Try the following test code:

<?phpfunction foo(&$var){    $var++;}$a=5;foo($a);echo $a;// $a is 6 here?>

and change nothing but the "&" at the function.You'll see that with reference, the echo above (which is outside the function) is echoing 6 rather than 5, and without reference, you're echoing 5.

Link to comment
Share on other sites

Yes I understand it now. Although I cannot figure out where this might be useful.

Link to comment
Share on other sites

Well... in all honesty, if you use classes, then indeed, you can pretty much do anything with them as opposed to doing it with references.References are one of those remains from pre-OOP times. In compiled languages (ala C and C++), they are also useful for reducing memory footprint (since you have one value being passed around, as opposed to it being copied around), but in languages like PHP, the runtime (in this case - the PHP interpreter) takes care about ensuring only a single value is passed around, and gets copied only if your code requires it (e.g. if you modify the value within the function).If you take objects out of the equasion, references are useful when you need a function to modify data that will be used later AND need its return value to signal something else. You can see examples with PHP's own functions, such as preg_match_all(). On the one hand, the return value is the number of matches, and on the other, $matches is a variable that's modified to contain each match.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...