PHP variable scope and address reference _ PHP Tutorial

Source: Internet
Author: User
Let's talk about the PHP variable scope and address reference. Scope concept: variables can be declared anywhere in the PHP script. However, the location of declared variables will greatly affect the scope of the access variables. The accessible scope is called the scope concept:

Variables can be declared anywhere in the PHP script. However, the location of the declared variables will greatly affect the scope of the access variables. The accessible range is called scope.

Common variables include local variables, global variables, and static variables.

1. local variable: the variable declared in the function, which is saved in the memory stack, so the access speed is very fast. Valid only in the function.

2. global variables: unlike local variables, global variables can be accessed anywhere in the program. You only need to add the GLOBAL keyword before the variable to recognize it as a GLOBAL variable. Valid throughout the php file.

3. static variables: modify the variables that only exist in the function scope with the static modifier. the values of the variables do not disappear after the function is executed. Note: Initialization cannot be performed again after initialization, and values cannot be assigned using expressions.

The code is as follows:


Function test ()
{
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.

$ B = $ B + 1;

Echo $ B;
}
Test (); // This statement outputs the value of $ B as 1.
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, but his statement is outside the method.

Appendix B: scope in js: use var aa = 'XXX'; declare global variables 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: References

PHP Reference: adding a reference in &. php to a variable, function, or object is to use different names to access the content of the same variable.

1. variable reference:

The code is as follows:


$ A = "ABC ";

$ B = & $;

Echo $ a; // output here: ABC

Echo $ B; // output here: ABC

$ B = "EFG ";

Echo $ a; // Here, the value of $ a is changed to EFG, So EFG is output.

Echo $ B; // output EFG here

2. function address transfer call

The code is as follows:


Function test (& $)

{

$ A = $ a + 100;

}

$ B = 1;

Echo $ B; // output 1

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.

Echo"
";

Echo $ B; // output 101

3. function reference and return

The code is as follows:


Function & test ()
{

Static $ B = 0; // declare a static variable

$ B = $ B + 1;

Echo $ B;

Return $ B;

}

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

$ A = 5;

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

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

$ A = 5;

$ 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

The code is as follows:


$ A = 1;

$ B = & $;

Unset ($ );

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. 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 direction is not implemented by the user, but implemented through the zend core. The php reference adopts the principle of "write copy, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

The code is as follows:


$ 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.

Callback can declare variables at any position of the PHP script. However, the location of the declared variable will greatly affect the scope of the access variable. This accessible range is called scope...

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.