Reference in PHP means access to the same variable content with a different name. Unlike Pointers in C, for example, you can’t do pointer operations on references, which are not actual memory addresses.
Note that in PHP, variable names and variable contents are not the same, so the same content can have different names.
References in PHP allow two variables to point to the same thing, for example:
1 <? php 2 $a = &$b; 3? >Copy the code
This means that $a and $b refer to the same variable, where $a and $b are exactly the same, not that $A refers to $B or vice versa, but that $a and $b refer to the same place.
Such as:
[php]
view plain
copy
print
?
- <?
- $a = “ABC”;
- $b = &$a;
- echo $a; // output :ABC
- echo $b; // output :ABC
- $b= “EFG”;
- echo $a; $a = EFG; $a = EFG
- echo $b; // Output EFG here
- ? >
<? $a = "ABC"; $b = &$a; echo $a; Echo $b; $b= "EFG"; echo $a; Echo $b; echo $b; // Where to print EFG? >Copy the code
[php]
view plain
copy
print
?
- function test(&$a)
- {
- $a = $a+100;
- }
- $b=1;
- echo $b; / / output 1
- test($b); $a = $b; $b = $a; $b = $a
- echo “<br>”;
- echo $b; / / output 101
Function test(&$a) {$a = $a+100; } $b=1; echo $b; // output 1 test($b); Echo "<br>"; echo "<br>"; echo $b; / / output 101Copy the code
[php]
view plain
copy
print
?
- function &test()
- {
- static $b = 0; // Declare a static variable
- $b = $b + 1;
- echo $b;
- return $b;
- }
- $a=test(); // This statement outputs the value of $b as 1
- $a=5;
- $a=test(); // This statement outputs the value of $b as 2
- $a=&test(); // This statement outputs the value of $b as 3
- $a=5;
- $a=test(); // This statement outputs $b with a value of 6
function &test() { static $b = 0; // declare a static variable $b = $b + 1; echo $b; return $b; } $a=test(); $b = 1 $a=5; $a=test(); $a=&test(); $a=&test(); $b = 3 $a=5; $a=test(); // This statement outputs $b with a value of 6Copy the code
$a=test(); $a=&test(); $a=&test(); A reference return is used when you want to use a function to find which variable a reference should be bound to. $a=test(); $a=test(); $a=&b; $a=&b; $a=&b; $a=&b; $a=&test(); $a=&test(); $a=5; In the future, the value of $b will change to 5. I’m using static variables just to make sure you understand that function reference returns are more common in objects
Object call
[php]
view plain
copy
print
?
- <?
- class a{
- var $abc=”ABC”;
- }
- $b=new a;
- $c=$b;
- echo $b->abc; // output ABC
- echo $c->abc; // output ABC
- $b->abc=”DEF”;
- echo $c->abc; // output DEF
- ? >
<? class a{ var $abc="ABC"; } $b=new a; $c=$b; echo $b->abc; Echo $c-> ABC; $b-> ABC ="DEF"; echo $c->abc; // DEF? >Copy the code
In PHP, objects are copied by reference.
$b=new a; $c=$b; That’s the same thing as $b=new a; $c=&$b;
The default in PHP is to call objects by reference, but sometimes you might want to make a copy of an object and hope that changes to the original object will not affect the copy.
For this purpose, PHP defines a special method called __clone.
The role of reference
If the program is large, many variables refer to the same object, and you want to use “&” to manually clear the object, then use $var=null to clear it.
In addition, for large arrays in PHP, it is recommended to use “&” mode, which can save memory space.
Cancel the reference
When you unset a reference, you simply break the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. Such as:
1 <? php 2 $a = 1; 3 $b =& $a; 4 unset ($a); 5? >Copy the code
It doesn’t unset $b, it just unsets $A.
Global reference
Declaring a variable with global $var actually establishes a reference to the global variable. In other words, it’s the same thing as doing this:
1 <? php 2 $var =& $GLOBALS["var"]; 3? >Copy the code
This means, for example, that unset $var does not unset global variables.
$this
In an object’s method, $this is always a reference to the object that called it.
PHP addresses pointing (like Pointers) is not implemented by the user, but by the Zend core. PHP references the “copy-on-write” principle.
A variable or object pointing to the same address will not be copied unless a write operation is performed.
Popular speak
1. If there is the following code
$a=”ABC”;
$b=$a;
$a and $B are pointing to the same memory address
2. Add the following code to the previous code
$a=”EFG”;
Because the $a and $b points to memory data to rewrite again, at this time the Zend core automatically judge automatically produce a $$b a copy of data to apply for a piece of memory for storage