What is the function name added before the PHP custom function name? For details, write a demo.

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.