What is the role of adding & amp; Before the PHP function name? For details, add a small demo PHP function name before it & what is the role? For details, write a demo.
Reply content:
What is the function name prefix for PHP? For details, write a demo.
This is a function reference. The demo is as follows:
Function & foo () {static $ 'enter the code 'var = 0; return ++ $ var;} $ result1 = foo (); $ result2 = & foo (); foo (); echo $ result1; // output is 1 echo $ result2; // output is 3
In this case, result2 is a reference to the var variable in the function. Therefore, after foo is executed three times, the var value is 3 and result2 is 3. However, result1 only obtains the first return value for function execution, so it is 1.
If you use the & symbol but do not use it, the effect of this function is the same as that of not using this symbol.
Function foo1 () {static $ var = 0; return ++ $ var ;}$ result1 = foo1 (); foo1 (); echo $ result1; // output is 1
At this time, there is no difference between foo1 () AND foo (), but if you do not add the & Symbol and want to use the reference to return the result, an error will be reported.
Function foo2 () {static $ var = 0; return ++ $ var ;}$ result2 = & foo2 (); // error herefoo2 (); echo $ result2;