1. Basic Concepts
Value Assignment: When assigning the value of an expression to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, when the value of one variable is assigned to another variable, changing the value of one of the variables will not affect the other variable. Reference assignment: This means that the new variable is a simple reference (in other words, "becomes its alias" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa.
1<?PHP2 $a= ' A ';3 $b= ' B ';4 Echo"$a,$b;5 //show A, b6 $b=$a;//Transfer Value Assignment7 $a= ' A1 ';8 Echo"$a,$b;9 //Show A1,aTen $b=&$a;//Reference Assignment One $a= ' A2 '; A Echo"$a,$b; - //Show A2,a2 -?>
2. Effects of unset () on reference assignment variables and application
1<?PHP2 $a= ' 1 ';3 $b=&$a;4 Echo"$a,$b;5 //Show All6 unset($a);7 Echo"$a,$b;8 //$a is destroyed, but because $b still occupies the ' 1 ' parameter, $b can be called. 9?>
If the program is larger, the variable that references the same object is more, and you want to use the object to manually clear it, use the "&" method, and then clear it $var=null.
3. Assigning values to references in functions
1<?PHP2 functionNum (&$num)//Reference Assignment3 {4 $num++;5 return $num;6 }7 $a= 1;8 Echo $a.‘,‘;9 EchoNum$a).‘,‘;Ten Echo $a; One //Show 1,2,2 A?>
For this function, num (1) cannot be used, and the argument must be a variable.
4. A reference to a function returns
<?PHPfunction&Test () {Static $b= 0;//declare a static variable $b+ = 1; Echo $b.‘,‘; return $b;}$a= Test ();//This statement will output a value of 1 for the $b$a= 5;$a= Test ();//This statement will output a value of 2 for the $b$a=& test ();//This statement will output a value of 3 for the $b$a= 5;$a= Test ();//This statement will output a value of 6 for the $b?>
$a = Test () calls the function, just assigns the value of the function to $ A, and $ A does nothing to affect the $b in the function.
The function is called by a $ A = &test () method to point the memory address of the $b variable in the return $b to the same place as the memory address of the $ A variable.
That produces the equivalent effect ($a =& $b;) so change the value of $ A and change the $b at the same time.
5. References to Objects
<?PHPclassa{var $ABC= "ABC"; } $b=NewA; $c=$b; Echo $b->abc;//Output ABC here Echo $c->abc;//Output ABC here $b->ABC = "DEF"; Echo $c->abc;//Output def here?>
The assignment of an object in PHP5 is a reference procedure. The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b;
The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP5 defines a special method called __clone.
since PHP 5, new automatically returns references, so using =& here is obsolete and generates E_STRICT-level messages. (This paragraph was excerpted from http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/10/2173092.html)
6. Global References
<? PHP $a $GLOBALS ["a"];
When you declare a variable with the global $a, you actually establish a reference to the global variable. unset ($a) does not unset global variables.
7. Benefits of processing in large arrays
A very large array of $ A, if you use $b = $a, then memory usage will be one more times, if $b =& $a, then there is little impact, and faster than the former.
<? PHP $arr = array ( ' 0 ' = array ( ' 0 ' = Span style= "color: #0000ff;" >array , ' 1 ' = array ()); // first method $arr [0] [0] = 1 // second method $b =& $arr [0 ]; $b [0] = 1; ?
Value assignment and reference assignment in a detailed