Jump to content
jimfog

passing by reference

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?

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...