开发者

difference between function and &function [duplicate]

This question already has answers here: What does it mean to start a PHP function with an ampersand? 开发者_如何学JAVA (3 answers) Reference — What does this symbol mean in PHP? (24 answers) Closed 7 years ago.
function fun1();
function &fun1();

What is the difference between these??


If you put an ampersand before your function name, the function will return a reference to a variable instead of the value.

Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.

From the PHP Manual on Returning References.


The difference is how the return value is returned: Without & the return value is returned by value and with & the return value is returned by reference.

The latter implies that you need to use a variable in the return statement as you can only reference variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜