Discussion on scope of PHP variable and address reference

  • 2020-12-09 00:45:44
  • OfStack

Concept of scope:

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

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

1, local variables: variables declared in the function, it is stored in the memory stack, so access speed is very fast. It only works within the function.

2. Global variables: As opposed to local variables, global variables can be accessed anywhere in the program. A variable can be recognized as a global variable simply by prepending it with the keyword GLOBAL. Valid throughout the php file.

3. Static variables: static is used to modify variables that only exist in the scope of a function, whose value does not disappear after the function is executed. Note: initialization cannot be reinitialized, and cannot be assigned using an expression.


function test() 
{ 
static $b=0;// A declaration of a static variable, placed outside a function, is not used inside a function 
$b=$b+1; 
echo $b; 
} 
test();// This statement will print $b A value of 1  
test();// This statement will print $b The value of 2 

Note: static $b=0 this 1 assignment will only be performed when the variable is initialized the first time.

A: Static members and static methods in the class, almost only when called using the class name or self or parent plus: : xxx, they have the same scope as this one, but their declarations are outside of the method

B: js scope: use var aa= 'xxx'; What is declared outside the function is the global variable (with or without the modifier var). What is declared inside a function using var are local variables, and what is not decorated with var are global variables.

Attached C: About citation

PHP references: Simply precede variables, functions, or objects & The reference in.php is the content of the same variable that you want to access under a different name.

1. Variable reference:


$a="ABC"; 
$b =&$a; 
echo $a;// Here is the output :ABC 
echo $b;// Here is the output :ABC 
$b="EFG"; 
echo $a;// Here, $a The value of a EFG  So the output EFG 
echo $b;// Here is the output EFG

2, the address of the function call


function test(&$a) 
{ 
$a=$a+100; 
} 
$b=1; 
echo $b;// Output 1  
test($b); // Here, $b And what you pass to the function is actually $b The memory address of the variable's contents is changed in the function $a I can change the value of theta $b The value of the  
echo "<br>"; 
echo $b;// The output 101 

3, function reference return


function &test() 
{ 
static $b=0;// statement 1 Static variables  
$b=$b+1; 
echo $b; 
return $b; 
} 
$a=test();// This statement will print $b A value of 1  
$a=5; 
$a=test();// This statement will print $b The value of 2 
$a=&test();// This statement will print $b The value of 3 
$a=5; 
$a=test();// This statement will print $b The value of 6 

Parsing: Using $a=test() does not actually return a reference to the function. Just copy the return value of the function to $a without affecting $b. This is the same as a normal call.

Php states: $a= & test() is the return of a function reference. He points the memory address of the $b variable to the same place as the memory address of the $a variable. This is equivalent to $a= & $b;

4. Unquote


$a = 1; 
$b =& $a; 
unset ($a); 
echo $b;

The unset1 reference, which removes the binding between the variable name and the content of the variable, does not mean that the content is destroyed, and its value is still real.

5. global references: When you use global $var to declare a variable, you are creating a reference to a global variable. Global $val < = > $var= & $GLOBALS [' var '];

6. Object references: In the object's method, the object called by $this is the reference calling it

Note: The reference to address in php is not implemented by the user, but by the zend core. The reference of php adopts the principle of "write copy", that is, variables or objects pointing to the same address will not be copied unless a write operation takes place.


$a = 1; 
$b =$a; 

$a and $b both point to the same memory address, not that $a and $b use different memory.

If you now execute the sentence $a= "dsd" :$a and $b point to memory data that needs to be rewritten once, the zend core automatically determines. Automatically generates 1 copy of $a for $b and reapplies 1 block of memory for storage.


Related articles: