On the scope of PHP variable and the problem of address reference _php tutorial

Source: Internet
Author: User
Concept of scope:

Variables can be declared anywhere in the PHP script, but declaring the location of a variable greatly affects the scope of the access variable. This accessible scope is called scope.

The main commonly used include: local variables, global variables, static variables.

1, local variables: is the variable declared within the function, he is stored in the memory stack, so the access speed quickly. Valid only within a function.

2. Global variables: In contrast to local variables, global variables can be accessed from anywhere in the program. You can recognize a global variable by adding the keyword global before the variable. Valid within the entire PHP file.

3, static variable: With static decoration only exists in the function scope of the variable, after the function execution ends its value does not disappear. Note: You cannot initialize after initialization, and you cannot use an expression to assign a value.

Copy the Code code as follows:
function test ()
{
Static $b =0;//declaration of a statically variable, which is not used inside a function if it is declared outside the function.

$b = $b +1;

Echo $b;
}
Test ();//This statement outputs a value of 1 $b
Test ();//This statement outputs a value of 2 $b

Note: Static $b =0 This assignment will only be performed when the variable is initialized for the first time.

Attached a: static members and static methods in the class, almost just call the time uniformly use the class name or self or parent plus: XXX, their scope and this same, but his statement is outside the method

The scope of the attached B:JS makes: aa= ' xxx ' with var; the global variable is declared outside the function (with or without the modifier var). Using var inside a function declares a local variable, and a global variable is not decorated with var.

Attached C: About references

PHP References: A reference to a variable, function, or object before adding &.php to the content of the same variable with a different name.

1. References to variables:

Copy the Code code as follows:
$a = "ABC";

$b =& $a;

echo $a;//output here: ABC

echo $b;//output here: ABC

$b = "EFG";

echo $a;//The value of $ A here becomes EFG so the output EFG

echo $b;//Output EFG here

2, the function of the address call

Copy the Code code as follows:
Function test (& $a)

{

$a = $a +100;

}

$b = 1;

echo $b;//Output 1

Test ($b); Here $b passed to the function is actually the memory address of the variable content of $b, by changing the value of $ A in the function can change the value of $b

echo "
";

echo $b;//Output 101

3. A reference to the function returns

Copy the Code code as follows:
function &test ()
{

Static $b =0;//declaration of a statically variable

$b = $b +1;

Echo $b;

return $b;

}

$a =test ();//This statement outputs a value of $b of 1

$a = 5;

$a =test ();//This statement outputs a value of $b of 2

$a =&test ();//This statement outputs a value of $b of 3

$a = 5;

$a =test ();//This statement outputs a value of $b of 6

Parsing: The use of $a=test () is actually not a function of the reference returned. Simply copy the return value of the function to $ A, without affecting the $b. There is no difference between this call and the normal call.

PHP Rules: $a =&test () is the way to get a reference to the function is returned. He points the memory address of the $b variable to the same place as the memory address of the $ A variable. That is equivalent to $a=& $b;

4. dereference

Copy the Code code as follows:
$a = 1;

$b =& $a;

unset ($a);

Echo $b;

Parse: unset a reference, just cancels the binding between the variable name and the contents of the variable, does not mean that the content is destroyed, its value is still real.

5. Global reference: When declaring a variable using the global $var, it is actually establishing a reference to the global variable. Global $val <=> $var =& $GLOBALS [' var '];

6. Object reference: In the method of the object, the object that $this called is the reference that invokes it

Note: In PHP, the point of the address is not implemented by the user itself, but through the Zend core implementation, the PHP reference is the "write copy" principle, that is, unless a write operation, a pointer to the same address of the variable or object will not be copied.

Copy the Code code as follows:
$a = 1;

$b = $a;

Both the $a and $b point to the same memory address, not $ A and $b occupy different memory.

If you execute a $a= "DSD" Now: the memory data pointed to by the $a and $b needs to be re-written, and the Zend core is automatically judged. Automatically generates a $ A copy of the data for $b and re-applies a piece of memory for storage.

http://www.bkjia.com/PHPjc/621647.html www.bkjia.com true http://www.bkjia.com/PHPjc/621647.html techarticle Scope Concept: Variables can be declared anywhere in a PHP script, but declaring a variable's position greatly affects the scope of the access variable. This accessible scope is called scope ...

  • Related Article

    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.