Suppose there is such a function

function a() { static $var = 0; $var++; return $var; } 

and there is such a function

 function &b() { static $var = 0; $var++; return $var; } 

What is their difference?

Updated!

In the picture below, I tried to explain the understanding of each case. enter image description here If I somehow incorrectly explained or understood, write to me, I will correct it (This refers to the image attached above)

  • four
    The main difference is that for the second option you will get over your ears from your senior for a code review. And in general, you correctly answered php.net/manual/ru/language.references.pass.php . - E_p
  • @E_p That is, the value will be returned if the function is declared with &, but it is written $var = b(); Will the link be returned only if the ampersands are in 2 places? - MaximPro
  • Updated the question. - MaximPro
  • one
    “I will write additional questions as current issues are addressed” - additional questions need to be written with separate questions. So that the answer given by the participants does not suddenly become incomplete. - VladD
  • @VladD And what if the questions that appear then are part of an incomplete question initially? I specifically did this so that they would answer me consistently, and not be distracted by just what is not needed - MaximPro

3 answers 3

What you are asking is very easy to verify.
The short answer is: if there is NO ampersand before calling a function marked as a return by reference, it will return by value. If the returned result is passed to a function that takes an argument by reference, then the transfer occurs by reference.

http://ideone.com/4WPtNg

 <?php class Test { public $a = 111; function &change( $c ) { return $this->a; } }; function secret_function( &$arg ) { $arg = 999; } $test = new Test(); $prop = $test->change(); echo( "$prop $test->a \n" ); $prop = 555; echo( "$prop $test->a \n" ); $prop = &$test->change(); $prop = 777; echo( "$prop $test->a \n" ); secret_function( $test->change() ); echo( "$prop $test->a \n" ); 

Result:

 111 111 555 111 777 777 999 999 
  • Have you seen the picture I attached at the end of the link? - MaximPro
  • In addition, I do not agree with you "if there is no ampersand before calling a function marked as return by reference, then it will return by value," but what if we return the function to a call to another function where the argument is passing by reference =) PS In fact, if in the declaration there is an ampersand, then the function returns a reference to the value of the variable, it will simply assign the value referenced by the link - MaximPro
  • Have you seen this picture? Use imgur.com for posting images, or using SO standard tools. - ReinRaus
  • "return a function to a call to another function where the argument is passing by reference" better give a sample code, as you imagine. - ReinRaus
  • one
    The fact that this Vkontakt, which does not allow anyone except you to see this image. - ReinRaus

If you use an ampersand in front of the function name when defining it, the result of the function will not return a value, but a reference to the variable values.

  • Suppose we use a variant of the function where the declaration says &, repl.it/DtfE/0 You wrote that if we write & in the declaration, then the link returns, not the value, but something is not visible =) - MaximPro
  • function &a(&$c) { return $c; } $c = 1; $d = &a($c); $d++; echo $c; Here is an example of how it is used. As a result, we will see “2” on the screen, not “1” - carcinogen75
  • Well, you have an ampersand also in the assignment, and not just in the declaration, so your statement is not entirely correct! If the function returned a link, then an ampersand would not be needed in the assignment! Or something I do not understand? PS Manual read - MaximPro
  • Well, the link has returned, but to work with a variable by reference you need to use & . In general, I agree, it's all confused :) - carcinogen75
  • If you take an analogy with C ++, then what you said is the way it works there, the same pointers =) But this isn’t C ++ and there are even written links! = Pointers =) I ask why: if the reference is in the declaration, but not in the assignment. And if not in the assignment, then what becomes of the "return link". Does it evaporate? Or what ... the moment is not clear - MaximPro

In the second case, the function returns not the value of the variable, but a reference to it.

Returning by reference is used when you want to use a function to select a variable with which the reference should be associated.

Here is a more obvious example:

 function &collector() { static $collection = array(); return $collection; } $collection = &collector(); $collection[] = 'foo'; 
  • And instead of $collection = &collector(); this is $collection = collector(); How do you? - MaximPro
  • 2
    And where is it? The link will be returned only if there is an ampersand there and there. - YuS
  • That is, the value will be returned if the function is declared with &, but it is written $collection = collector(); - MaximPro
  • Updated the question. - MaximPro