What is the meaning of adding a number before defining a function?

Source: Internet
Author: User
When defining a function, what is the meaning of an ampersand? This is what I saw in the forum's E snail shoes post. it accurately describes the role of adding & amp; in front of the function and the reference and return of the specific effect function.

When defining a function, what is the meaning of the plus sign?

This is what I saw in the forum's E-snail shoes post. it accurately describes the role of adding & in front of the function and the specific effect.

Function Reference returnFirst look at the code:

  1. Function & test ()
  2. {
  3. Static $ B = 0; // declare a static variable
  4. $ B = $ B + 1;
  5. Echo $ B;
  6. Return $ B;
  7. }
  8.  
  9. $ A = test (); // This statement outputs the value of $ B as 1.
  10. $ A = 5;
  11. $ A = test (); // This statement outputs the value of $ B to 2.
  12.  
  13. $ A = & test (); // This statement outputs the value of $ B to 3.
  14. $ A = 5;
  15. $ A = test (); // This statement outputs a value of 6 for $ B.

The following explains:In this way, $ a = test (); does not actually return a function reference, which is no different from a common function call.

As for the reason:This is what PHP requires.

PHP requires the $ a = & test (); method to obtain the function reference and return. what is the reference return? (the PHP manual says: the return value of the reference is used when you want to use the function to find the variable on which the reference should be bound .)

The example above is as follows:$ A = test () is used to call a function. it only assigns the value of the function to $ a. any change made to $ a does not affect $ B in the function, but how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable, that is, the equivalent effect ($ a = & B;) is generated. Therefore, changing the value of $ a also changes the value of $ B. Therefore, after executing the following code, the value of $ B is changed to 5.

  1. $ A = & test ();
  2. $ A = 5;

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.