Why use the & sign 2 times?

I will give an example:

 function &func() /// Тут { static $static = 0; $static++; return $static; } $var1 = &func(); /// Тут echo "var1:", $var1; // 1 func(); func(); echo "var1:", $var1; // 3 

The documentation about this is not written! It would be more logical to write return &$static

And another question, what if we do not write an ampersand in the function specification?

And what will also be if we write an ampersand in the function indication, but not in the assignment?

  • If a variable has no reference ( & ), then the value will not be updated. Also with the function - there will always be 1 . - mix
  • @mix I did not understand anything, make it clearer, preferably with examples, this is not Russian - MaximPro
  • The & sign is called the “link” - mix
  • @mix yeah we know about it =) - MaximPro
  • then you know bad, just asking. The clearest example is in front of you, experiment and understand the need for a 2-ampersand. - mix

1 answer 1

Actually, no, in the documentation this is written (and quite detailed and clear in my opinion): http://www.php.net/manual/en/language.references.return.php Why? This syntax was invented. You probably wrote in C or C ++ earlier if it bothers you. Well, php is not C, the links in it work quite differently. How exactly - it is best to read, again, in the documentation.

and what if we do not write an ampersand in the function indication?

That will not return by reference, but by value. The ampersand sign is the only and sufficient condition to indicate that the function should return "by reference". In the "return" construction, no ampersands are needed.

And what will also be if we write an ampersand in the function indication, but not in the assignment?

This is a valid language design. You assign a value to a variable. Actually, in this case, an ampersand refers to an assignment rather than a function. With the same success, you can write, for example, $ var = & $ var2; - to make

  • PS The best way to test “what if” is to write a test case and execute it :) - Yury Sitnikov
  • Well, if there is no ampersand in any place, then there will be no linking to the link, I’m just wondering if the code inside will work differently, for example, if I just write an ampersand before the function name in the declaration ... it will be the same as the function without ampersand ? - MaximPro
  • Inside the function, everything will work in exactly the same way as when returning by value. The only exception is if the function is declared with an ampersand, then return must contain an expression that can be accessed by reference (variable, access to an array variable, call another function with return by value, etc.). Return a reference to a constant, for example, can not (directly). - Yury Sitnikov
  • The key differences will not be in the execution of the function, but in the use of the result of its execution. - Yury Sitnikov
  • Expressions can not be passed by reference! - MaximPro