What is the reference?
Referencing in PHP means accessing the contents of the same variable with a different name. This is not like a C pointer, but instead, the reference is a symbolic table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be viewed as hardlink in Unix file systems.
One: A reference to a variable
Copy Code code as follows:
<?php
$a = 100;
$b = & $a;
Echo $b; Output 100 here
echo $a; Here is the output of 100, which shows that $a, and $b values are all 100.
$b = 200;
echo $a; Output 200 Here
Echo $b; Output 200 here, which shows that they are using the same address. Change one, and the other will change accordingly.
?>
Two: Reference values in functions
Copy Code code as follows:
<?php
function main ($a, $b) {
$b = $a +100;
return $b;
}
Main (55,& $b); The $b is actually passing its memory address to the $b parameter in function main, changing the value of the outer $b by changing the parameter $b.
Echo $b; This will output 155,
?>
Three: object's reference Pass value
References to Objects
Copy Code code as follows:
?
Class club{
var $name = "Real Madrid";
}
$b =new Club;
$c = $b;
echo $b->name;//here output Real Madrid
echo $c->name;//here output Real Madrid
$b->name= "Ronaldo";
echo $c->name;//here output Ronaldo
?>
Dereference
When you unset a reference, only the binding between the variable name and the variable content is disconnected. This does not mean that the variable content has been destroyed. For example:
Copy Code code as follows:
<?php
$a = ' Ronaldo '
$b =& $a;
unset ($a);
?>
Not unset $b, just $a.
example, reference passing
test1.php
Copy Code code as follows:
<?php
/**
* Reference Delivery
The following can be passed by reference:
variables, such as foo ($a)
New statement, such as Foo (new Foobar ())
The reference returned from the function, for example:
*/
function foo (& $var)
{
$var + +;
}
$a = 5;
Legal
Foo ($a);
Foo (new StdClass ());
illegal use
function bar ()//Note the missing &
{
$a = 5;
return $a;
}
Foo (bar ()); Fatal error since PHP 5.0.5
Foo ($a = 5)//expression, not variable
Foo (5)//lead to fatal error
?>
test2.php
Copy Code code as follows:
<?php
Function test (& $a)
{
$a = $a +100;
}
$b = 1;
echo $b;//Output 1
Test ($b); Here $b passed to the function is actually the $b variable content of the memory address, by changing the value of $a in the function can change the value of $b
echo "<br>";
echo $b;//Output 101
/*****************************
*
* Here you need to note that the Call_user_func_array parameter is required &
*
* ****************************/
"Test ($b) above; "In the $b, do not precede the & symbol, but in the function" Call_user_func_array ", to refer to the argument, you need the & symbol, as shown in the following code:
Function A (& $b) {
$b + +;
}
$c = 0;
Call_user_func_array (' A ', Array (& $c));
Echo $c;
Output 1
?>
Reference returns
Reference return is used when you want to use a function to find the reference to which variable it should be bound. Do not use return references to increase performance, the engine is smart enough to optimize itself. Return the reference only if there is a reasonable technical reason! To return a reference, use this syntax
Copy Code code as follows:
<?php
function &test ()
{
Static $b =0;//declares a statically variable
$b = $b +1;
Echo $b;
return $b;
}
$a =test ()//This statement will output $b value of 1
$a = 5;
$a =test ()//This statement will output $b value of 2
$a =&test ();//This statement will output the $b value of 3 the memory address of the $b variable in the return $b is pointed to the same place as the memory address of the $a variable.
$a = 5; The value of the $b variable in the return $b has been changed
$a =test ()//This statement will output $b value of 6
?>
Explain:
$a=test in this way (); What you get is not the return of a reference to a function, which is no different from a normal function call. As for the reason: this is the PHP rule
PHP rules through $a=&test (); The way to get the function is to return the reference
As for what is the reference return (PHP manual says: Reference returns used when you want to use a function to find the reference should be bound to which variable above.) This shit is killing me for half a day.
The example above is to explain
Calling a function $a =test () simply assigns the value of the function to $a, and $a does nothing to affect the $b in the function
Instead of calling the function by $a=&test (), his role is to point the memory address of the $b variable in the return $b to the same place as the $a variable's memory address
That produces the equivalent effect ($a =& $b;) So changing the value of the $a also changes the value of the $b, so in the execution of the
$a =&test ();
$a = 5;
Later, the value of $b changed to 5.
Here is to let you understand the function of reference to return to use static variables, in fact, the function of reference return is more used in the object
An interesting example of this is seen on the Oschina:
Copy Code code as follows:
<?php
$a = Array (' Abe ', ' Ben ', ' cam ');
foreach ($a as $k =>& $n)
$n = Strtoupper ($n);
foreach ($a as $k => $n)//Notice NO reference here!
echo "$nn";
Print_r ($a);
?>
would result in:
ABE
Ben
Ben
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)
Explanation: Loop through the second foreach as follows:
Array
(
[0] => ABE
[1] => BEN
[2] => ABE
)
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)
Because there is no unset ($n), it always points to the last element of the array, and the first loop of the second foreach $n, that is $a[2, to Abe, the second cycle to Ben, and the third time Ben.