Home >Backend Development >PHP Tutorial >Detailed explanation of php reference & symbol
PHP reference (that is, adding an ampersand in front of a variable, function, object, etc.)
Reference in PHP means: different names access the same variable content.
Variable reference
PHP reference Allows you to use two variables to point to the same content
Example 1:
$a="2010";
$b =&$a;
echo $a;//Output here :2010
echo $b;//Output here: 2010
$b="2012";
echo $a;//The value of $a here changes to 2012 so the output
echo $b;//Here Output 2012
?>
Example 2:
$a = "date";
$b = &$a;
echo $a; // date
echo $b ; // date
$b = "date1";
echo $a; // date1
echo $b; // date1
unset($a);
echo $b; // date1
?>
From the above two examples, we can see that giving the memory address of $b to $b is not a simple assignment. So any operation on $b
will also affect $a
Another way of saying it is to add an alias $b to $a. If $a is deleted, only the name of the variable is deleted, but the variable is not deleted. The content of this variable can still be displayed using aliases. (As shown in the picture)
function call by address
Example 3:
function test(&$a)
{
$a=$a+100;
}
$b=1;
echo $b;//Output 1
//What $b passes to the function is actually the memory address where the variable content of $b is located, by changing the value of $a in the function You can change the value of $b
test($b);
echo $b;//Output 101
?>
If you test(1); here, an error will occur
Explanation Parameters can only be variables, constants do not have address passing.
Function reference return
Function reference return is mostly used in objects. It is convenient to understand here using static variables as an example
Example 4:
function &test()
{
static $b=0;//Declare a static variable
$b=$b+1;
echo $b;
return $b;
}
//This statement will output the value of $b is 1
$a=test();
$a=5;
$a=test();//This statement will output the value of $b as 2
$a=&test() ;//This statement will output the value of $b as 3
$a=5;
$a=test();//This statement will output the value of $b as 6
Comment, this function is There is output and there is also a return value.
$a = test(); It just assigns the return value $b of function test to $a. It is just a normal assignment, not a function reference return. So no matter what operations $a does, it will not affect $b.
$a = &test(); The function is to point the memory address of $b and the memory address of $a to the same place, which will produce an effect similar to $b = &$a. If the value of $a changes, Even if it becomes 5, it will also affect the value of $b. So when executing $a = &test(); $a = 5, there is $b = 5, and after function processing, $b = 6 is output;
?>
Reference of the object
Example 5:
class a{
var $abc="ABC";
}
$b=new a;
$c=$b;
echo $b->abc;// Output ABC here
echo $c->abc;//Output ABC here
$b->abc="DEF";
echo $c->abc;//Output DEF here
?> ;
The above code is the effect of running in PHP5. In PHP5, object copying is achieved through references.
$b=new a; $c=$b; in the above column is actually equivalent to $b=new a; $c=&$b;
The default in PHP5 is to call objects by reference, but sometimes you may You want to create a copy of an object, and hope that changes to the original object will not affect the copy. For such purposes, PHP defines a special method called __clone.
The role of references
If the program is relatively large, there are many variables referencing the same object, and you want to clear it manually after using the object, it is recommended to use the "&" method, and then use $var=null to clear it . At other times, just use the default method of php5.
In addition, for the transfer of large arrays in php5, it is recommended to use the "&" method, after all, it saves memory space.
Unreference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the variable contents are destroyed.
For example:
$a = 1;
$b =& $a;
unset ($a);
?>
will not unset $b, just $a .
You can refer to the variable reference section
global reference
When you declare a variable with global $var, you actually create a reference to the global variable.
It is equivalent to the following code:
$var =& $GLOBALS["var"];
?>
This means that, for example, unset $var will not unset Global variables.
$this
In a method of an object, $this is always a reference to the object that calls it.
Additional instructions
The pointing (similar to pointer) function of the address in php is not implemented by the user himself, but is implemented by the Zend core. The reference in php adopts the principle of "copy on write", that is, it will not be copied unless a write operation occurs. , for other operations, variables or objects pointing to the same address will not be copied.
Suppose there is the following code:
$a="ABC";
$b=$a;
Ps: I personally think it should be $b = &$a so that $a and $b point to the same Memory address, but this is what the information I referenced says. At present, I don’t know much about &. If any friends have different opinions, you can put them forward. Thank you
At this time, $a and $b are both Point to the same memory address, instead of $a and $b occupying different memories
If you add it to the above code, the following code
$a="EFG";
"write" here Operation
Since the data in the memory pointed to by $a and $b needs to be rewritten, the Zend core will automatically judge at this time, automatically produce a data copy of $a for $b, and re-apply for a piece of memory for storage.