Analysis of PHP variable scope and address reference

Source: Internet
Author: User
Tags variable scope
Analysis of PHP variable scope and address reference

  1. Function test ()
  2. {
  3. Static $ B = 0; // declare a static variable. if it is placed in the external declaration of the function, it cannot be used inside the function.
  4. $ B = $ B + 1;
  5. Echo $ B;
  6. }
  7. Test (); // This statement outputs the value of $ B as 1.
  8. Test (); // This statement outputs the value of $ B to 2.

Note: The value assignment operation static $ B = 0 is only executed when the variable is initialized for the first time. Appendix A: static members and static methods in the class. they only use the class name or self or parent Plus: xxx during the call. Their scope is the same as this, however, his statement is attached to the external Method B: the scope in js makes: use var aa = 'XXX '; the global variables declared outside the function (whether or not there is a modifier var ). Local variables are declared using var in the function, and global variables are not modified using var. Appendix C: PHP Reference: add a reference in &. PHP before a variable, function, or object to access the content of the same variable with different names.

1. variable reference:

  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

2. function address transfer call

  1. Function test (& $)
  2. {
  3. $ A = $ a + 100;
  4. } // Bbs.it-home.org
  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

3. function reference and 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.

Resolution: $ a = test () is not actually a function reference. Just copy the return value of the function to $ a without affecting $ B. In this way, the call is no different from the normal call. Php rules: $ a = & test () is returned as a function reference. The memory address of the $ B variable and the memory address of the $ a variable point to the same place. That is, equivalent to $ a = & $ B;

4. cancel the reference

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

Resolution: unset is a reference. it only removes the binding between the variable name and the content of the variable. it does not mean that the content is destroyed, and its value still exists.

5. global reference: when using global $ var to declare a variable, a reference to the global variable is actually created.

  1. Global $ val <=> $ var = & $ GLOBALS ['var'];

6. object reference: in the object method, $ this calls all the objects that call it. note: In php, the address pointing is not implemented by the user, it is implemented through the zend core. The php reference adopts the "write copy" principle, that is, unless a write operation occurs, the variables or objects pointing to the same address will not be copied. Example:

$ A = 1; $ B = $;

Both $ a and $ B point to the same memory address, rather than $ a and $ B occupy different memory. If you execute $ a = "dsd": the memory data pointed to by $ a and $ B needs to be re-written, then the zend core automatically determines. A $ a data copy is automatically generated for $ B, and a memory is re-applied for storage.

This article describes the scope of php variables and the content referenced by php. I hope it will be helpful to you.

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.