This article mainly introduces the scope of PHP variables and address reference issues, a friend in need can refer to the
The concept of scope: Can declare variables anywhere in the PHP script, but the location of the declared variables can greatly affect the scope of the access variable. The scope that can be accessed is known as scopes. main commonly used include: local variables, global variables, static variables. 1, local variables: is declared in the function of the variable, he kept in the memory of the stack, so the access is very fast. Valid only within a function. 2, global variables: In contrast to local variables, global variables can be accessed anywhere in the program. As long as you add the keyword global to the front of the variable, you can recognize it as a global variable. Valid throughout the PHP file. 3, static variables: Using static to decorate variables that exist only in the scope of the function, the value does not disappear after the function is executed. Note: Cannot initialize again after initialization and cannot be evaluated with an expression. The code is as follows: the function test () { static $b =0;//declare static variables, which are declared outside the function, and are not used within functions $b = $b +1; echo $b; nbsp } test ();//This statement outputs the value of the $b to 1 test ();//This statement outputs $b value of 2 NOTE: static $b =0 This assignment will only be the first time the variable is initially When it is done. attached A: static members and static methods in a class, almost just when the call is unified using the class name or self or parent:: XXX, their scope is the same as this, but his statement is in the outside of the method of the scope of the attached b:js: with var aa= ' xxx '; The global variable (whether or not with the modifier Var) is declared outside the function. A local variable is declared using var within a function, and a global variable is not decorated with var. C: About references PHP references: The reference to a variable, function, or object plus &.php is to access the contents of the same variable with a different name. 1, variable reference: code is as follows: $a = "ABC"; $b =& $a; echo $a;//here Output:abc echo $b;//Here Output:abc $b = "EFG"; ECHo $a///Here The value of $a is changed to EFG so output efg echo $b;//Here Output EFG 2, the function's calling code is as follows: Functions Test (& $a) &nbs P { $a = $a +100; } $b =1; echo $b;//output 1 test ($b); Here $b pass to the function is actually the $b variable content in the memory address, by changing the value of $a in the function can change the value of $b echo "<br>"; echo $b;//Output 101 3, function reference returns the code as follows: Functions &test () { static $b =0;//Declare a static variable & nbsp $b = $b +1; echo $b; return $b; } $a =test ();//This statement outputs the $b value of 1   ; $a =5; $a =test ();//This statement outputs $b value of 2 $a =&test ();//This statement outputs the value of $b 3 $a =5; &nb Sp $a =test ()///This statement will output the value of $b 6 parsing: Using $a=test () does not actually return the reference to the function. Simply copy the return value of the function to the $a without affecting the $b. This call is no different from a normal call. PHP Rules: $a =&test () method is the function of the reference to return. 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 code as follows: $a = 1; $b =& $a; unset ($a); echo $b; parsing: unset a reference that cancels the binding between the variable name and the contents of the variable, does not mean that the content is destroyed and its value is real. 5, Global reference: When declaring a variable using global $var , a reference to a global variable is actually established. Global $val <=> $var =& $GLOBALS [' var ']; 6, object reference: In the object's method, $this called the object is called its reference Note: PHP to address the point is not implemented by the user itself, but through the Zend Core implementation, PHP reference to use the "write copy" principle, Is that unless a write occurs, a variable or object that points to the same address is not copied. Code as follows: $a = 1; $b = $a; $a and $b all point to the same memory address, not $a and $b occupy different memory. If you now execute a $a= "DSD": the memory data pointed to by $a and $b need to be written again, at which point the Zend core is automatically judged. Automatically generate a $a copy of the data for $b and reapply a piece of memory for storage.