取消引用

当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:

<?php
$a
= 1;
$b =& $a;
unset(
$a);
?>

不会 unset $b,只是 $a

再拿这个和 Unix 的 unlink 调用来类比一下可能有助于理解。


add a note add a note User Contributed Notes
rustin dot phares at hotmail dot com
13-Mar-2006 02:31
If you wish to unset both variables, you will need to unset the last referenced variable of that condition.

<?php
$a
= 1;
$b =& $a;
unset(
$b);
?>

* These must be in a reference->copy hierarchy in order to unset; example:

<?php
$a
= 1;
$b =& $a;
$c =& $b;
unset(
$c);
?>

This will not work:

<?php
$a
= 1;
$b =& $a;
$c = $b;
unset(
$c);
?>

* Only $c is unset in the above example, meaning that both variables $b and $a are still assigned "1".
libi
24-Jan-2006 04:20
clerca at inp-net dot eu dot org
"
If you have a lot of references linked to the same contents, maybe it could be useful to do this :
<?php
$a
= 1;
$b = & $a;
$c = & $b; // $a, $b, $c reference the same content '1'

$b = NULL; // All variables $a, $b or $c are unset
?>

"

------------------------

NULL will not result in unseting the variables.
Its only change the value to "null" for all the variables.
becouse they all points to the same "part" in the memory.
clerca at inp-net dot eu dot org
26-Nov-2005 04:33
If you have a lot of references linked to the same contents, maybe it could be useful to do this :
<?php
$a
= 1;
$b = & $a;
$c = & $b; // $a, $b, $c reference the same content '1'

$b = NULL; // All variables $a, $b or $c are unset
?>

I haven't test this trick a lot, but well, it seems to work greatly.