Php reference (&) symbol details

Source: Internet
Author: User
Php reference (& amp;) symbol details

  1. $ A = "ABC ";
  2. $ B = & $;
  3. Echo $ a; // output here: ABC
  4. Echo $ B; // output here: ABC
  5. $ B = "EFG ";
  6. Echo $ a; // Here, the value of $ a is changed to EFG, So EFG is output.
  7. Echo $ B; // output EFG here
  8. ?>

2. I will not say much about calling the address transfer function. the following code is provided directly.

  1. Function test (& $)
  2. {
  3. $ A = $ a + 100;
  4. }
  5. $ B = 1;
  6. Echo $ B; // output 1
  7. Test ($ B); // here, $ B actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.
  8. Echo"
    ";
  9. Echo $ B; // output 101
  10. ?>

Note: test (1); errors occur.

3. function reference return

  1. Function & test ()

  2. {
  3. Static $ B = 0; // declare a static variable
  4. $ B = $ B + 1;
  5. Echo $ B;
  6. Return $ B;
  7. }

  8. $ A = test (); // This statement outputs the value of $ B as 1.

  9. $ A = 5;
  10. $ A = test (); // This statement outputs the value of $ B to 2.

  11. $ A = & test (); // This statement outputs the value of $ B to 3.

  12. $ A = 5;
  13. $ A = test (); // This statement outputs a value of 6 for $ B.
  14. ?>

Explanation: 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: PHP requires that $ a = & test (); is used to obtain the function reference and return. As for what is reference return (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .).

$ A = test () method to call a function. it only assigns the value of the function to $, $ a does not affect $ B in the function and calls the function in the $ a = & test () method, his role is to direct the memory address of the $ B variable in return $ B to the memory address of the $ a variable to the same place, which produces the equivalent effect ($ a = & B ;) therefore, changing the value of $ a also changes the value of $ B.

  1. Class {
  2. Var $ abc = "ABC ";
  3. }
  4. $ B = new;
  5. $ C = $ B;
  6. Echo $ B-> abc; // output ABC here
  7. Echo $ c-> abc; // output ABC here
  8. $ B-> abc = "DEF ";
  9. Echo $ c-> abc; // output DEF here
  10. ?>

The above code is executed in PHP5. in PHP5, object replication is implemented through reference. In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B; in PHP5, the object is called by reference by default, however, sometimes you may want to create a copy of an object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.

5. if the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then clear it in the form of $ var = null. in other cases, use the default php5 method. in addition, we recommend that you use the "&" method for transferring large arrays in php5 to save memory space.

6. cancel the reference. when you unset a reference, you only disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

  1. $ A = 1;
  2. $ B = & $;
  3. Unset ($ );
  4. ?>

Not unset $ B, just $.

7. global reference when a variable is declared using global $ var, a reference to the global variable is actually created. It is the same as this:

  1. $ Var = & $ GLOBALS ["var"];
  2. ?>

This means that, for example, unset $ var does not unset global variables.

$ This is always a reference to the object that calls an object in the method of an object.

In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "copy at Write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

1. if the following code exists:

$ A = "ABC"; $ B = $;

In fact, both $ a and $ B point to the same memory address, not $ a and $ B occupy different memory.

2. add the following code on the basis of the above code:

$ A = "EFG ";

Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core automatically determines that $ B will generate a $ a data copy, apply for a new memory for storage.

This article describes the usage of the reference symbol & in php and hopes to help you. Articles you may be interested in: php reference example of passing value details php reference example code php reference example of the PHP reference about the php variable reference, function transfer address and object reference example parsing php reference passing value for more information, see the differences between passing a value in php and transferring a reference through an instance. For more information, see the efficiency of php address reference through an instance. For more information about changing the variable value of a php address, see references in PHP, "&" explanation

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.