The reference of php is to add the & symbol in front of the variable or function, object, etc. The reference in PHP means: different names access the same variable content, the following describes how to use PHP reference, what is needed Friends can refer to the difference between
and pointers in C language. The pointer in C language stores the reference of the address variable where the content of the variable is stored in memory.
The reference of PHP allows you to use two variables to point to the same content
$a=”ABC”;
$b =&$a;
echo $a;//here
output: ABC
echo $b;// Output here: ABC
$b=”EFG”;
echo $a;//here the value of $a becomes EFG so output EFG
echo $b;//output EFG
function here
I will not say more about the address call
<?php
function test(&$ a) {
$a = $a + 100;
}
$b = 1;
echo $b; //
output 1
test($b); //Here, 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, the value of $b can be changed
echo “
“;
echo $b; //Output 101
?>
It should be noted that if test(1); is wrong here, the reason is to think for yourself
The reference return of the function
Look at the code first
<?php
function &test() {
static $b = 0; //Declare a static variable
$b = $b + 1;
echo $b;
return $b;
}
$a = test(); //This statement will
output The value of $b is 1
$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
?>
Let’s explain it below:
In this way, $a=test(); What you get is actually not a function reference return, which is no different from ordinary function calls. As for the reason: This is php It is stipulated that
php stipulates that what is obtained through $a=&test(); is the reference return of the function
As for what is a reference return (the PHP manual says: reference return is used when you want to use a function to find a reference Which variable should be bound to. ) I didn’t understand this nonsense for a long time
Using the above example to explain is
$a=test() way to call the function, It just assigns the value of the function to $a, and any change in $a will not affect $b in the function. When calling the function through $a=&test(), its function is to return the value in $b The memory address of the $b variable and the memory address of the $a variable point to the same place, which produces an effect equivalent to this ($a=&b;) So changing the value of $a also changes the value of $b so in After executing
$a=&test();
$a=5;
, $ The value of b has changed to 5.
This is to let everyone understand the reference return of the function to use the static variable. In fact, the reference return of the function is mostly used in the object
Object reference
<?php
class a {
var $abc = “ABC”;
}
$b = new a;
$c = $b;
echo $b->abc; //Here
output ABC
echo $c->abc; // Here output ABC
$b->abc = “DEF”;
echo $c->abc; //Here output DEF
?>
The above code is the running effect in PHP5
/>
The replication of objects in PHP5 is achieved by reference. In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b;
The default in PHP5 is to call the object by reference, but sometimes you may Want to create 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 relatively large, there are many variables that refer to the same object, and you want to manually clear the object after using it, I personally recommend using “&” way, and then clear it with $var=null. Otherwise, use the default method of php5. In addition, for the transmission of large arrays in php5, it is recommended to use the “&” method, after all, to save memory space.
Unset
When you unset a reference, you just break the binding between the variable name and the variable content . This doesn’t mean the variable contents are destroyed. For example:
$a = 1;
$b =& $a;
unset ($a);
will not unset $b, just $a.
global reference
When declaring a variable with global $var actually creates a reference to the global variable . That is the same as doing this:
$var =& $GLOBALS[“var”];
This means, for example, unset $var will not unset global variables.
$this
In an object method, $this is always a reference to the object that called it.
//Here comes another small episode
The function of pointing to the address (similar to a pointer) in php is not implemented by the user himself, but by the Implemented by the Zend core, references in php use the “copy-on-write” principle, that is, variables or objects pointing to the same address will not be copied unless a write operation occurs.
Plainly speaking
1: If there is the following code
$a=” ABC”;
$b=$a;
In fact, at this time $a and $b both point to the same memory address instead of $a and $b occupiedDifferent memory 2: If you add the following code on the basis of the above code
$a=”EFG”;
The above is the whole of this article The content, I hope it will be helpful to everyone’s study.
Related recommendations:
PHPThe benefits of adding a namespace are convenient for automatic loading
PHP Error level setting and principle analysis
break statement of php process control
php process control The continue statement
The above is the detailed content of the detailed explanation of the reference symbol (&) in PHP, and more Please pay more attention to other related articles!