The concept of scope:
Variables can be declared 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.
The main common include: local variables, global variables, static variables.
1, local variables: is declared in the function of the variable, he saved in the memory stack, so access speed. 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 variable: Use static to decorate a variable that exists only in the scope of a function, and its value does not disappear after the execution of the function ends. Note: Cannot initialize again after initialization and cannot be evaluated with an expression.
Copy Code code as follows:
function test ()
{
Static $b =0;//Declare the statically variable, which is not available inside the 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 is performed only when the variable is first initialized.
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 outside the method
Attached to the scope of B:js: var aa= ' xxx '; the global variable (whether or not with 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.
Attached C: About reference
PHP Reference: The reference to a variable, function, or object in front of the &.php is to access the contents of the same variable with a different name.
1, the reference to the variable:
Copy Code code as follows:
$a = "ABC";
$b =& $a;
echo $a//here output: ABC
echo $b//Here Output: ABC
$b = "EFG";
echo $a//The value of $a here becomes EFG so output EFG
echo $b//Here output EFG
2, the function of the address call
Copy 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 $b variable content of 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, the function of the reference to return
Copy Code code as follows:
function &test ()
{
Static $b =0;//declares a statically variable
$b = $b +1;
Echo $b;
return $b;
}
$a =test ()//This statement will output $b value of 1
$a = 5;
$a =test ()//This statement will output $b value of 2
$a =&test ()//This statement will output $b value of 3
$a = 5;
$a =test ()//This statement will output $b value of 6
Parsing: Using $a=test () is not actually returned by a reference to a 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 () way to get the function of the reference 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, cancel the reference
Copy Code code as follows:
$a = 1;
$b =& $a;
unset ($a);
Echo $b;
Parsing: unset A reference, just canceling 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 the global $var, a reference to the global variable is established. Global $val <=> $var =& $GLOBALS [' var '];
6. Reference to an object: In the object's method, the object that $this call is the reference to which it is invoked
Note: PHP to address the point is not implemented by the user itself, but through the Zend Core implementation, PHP reference to the "write copy" principle is that, unless a write operation, point to the same address variables or objects will not be copied.
Copy Code code as follows:
$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.