引用的解释
在线手册:中文 英文
PHP手册

取消引用

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

<?php
$a 
1;
$b =& $a;
unset(
$a);
?>
不会 unset $b,只是 $a

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


引用的解释
在线手册:中文 英文
PHP手册
PHP手册 - N: 取消引用

用户评论:

lowlight1974 at gmail dot com (20-Sep-2011 01:28)

Unsetting array and objects does NOT work when trying to unset them.  You have to iterate through them in order to empty them out, akin to cleaning out memory in c/c++.  Here are some examples:

<?php
echo "<pre>";
$a->something = true;
echo
"Running test(\$a)\n";
test($a);
//At this point $a still has the something=true values
var_dump($a);

echo
"\nRunning nuke_me(\$a)\n";
nuke_me($a);
//At this point, the values in $a have been deleted.

var_dump($a);
echo
"</pre>";

//Now, one would think that $a would be unset after running test().  But in this instance, $a is UNTOUCHED.

//Call $a by reference, name it something else for giggles
function test(&$trash)
{
   unset(
$trash);
}

function
nuke_me($var)
{
   if (
is_object($var) || is_array($var))
   {
      foreach (
$var AS $key => $value)
      {
         if (isset(
$key))
         {
            if(
is_object($var))
            {
               unset(
$var->$key);
            }

            if(
is_array($var))
            {
               unset(
$var[$key]);
            }
         }
      }
   }
   else
   {
       unset(
$var);
   }
}
?>

ojars26 at NOSPAM dot inbox dot lv (03-May-2008 03:41)

Simple look how PHP Reference works
<?php
/* Imagine this is memory map
 ______________________________
|pointer | value | variable              |
 -----------------------------------
|   1     |  NULL  |         ---           |
|   2     |  NULL  |         ---           |
|   3     |  NULL  |         ---           |
|   4     |  NULL  |         ---           |
|   5     |  NULL  |         ---           |
------------------------------------
Create some variables   */
$a=10;
$b=20;
$c=array ('one'=>array (1, 2, 3));
/* Look at memory
 _______________________________
|pointer | value |       variable's       |
 -----------------------------------
|   1     |  10     |       $a               |
|   2     |  20     |       $b               |
|   3     |  1       |      $c['one'][0]   |
|   4     |  2       |      $c['one'][1]   |
|   5     |  3       |      $c['one'][2]   |
------------------------------------
do  */
$a=&$c['one'][2];
/* Look at memory
 _______________________________
|pointer | value |       variable's       |
 -----------------------------------
|   1     |  NULL  |       ---              |  //value of  $a is destroyed and pointer is free
|   2     |  20     |       $b               |
|   3     |  1       |      $c['one'][0]   |
|   4     |  2       |      $c['one'][1]   |
|   5     |  3       |  $c['one'][2]  ,$a | // $a is now here
------------------------------------
do  */
$b=&$a// or  $b=&$c['one'][2]; result is same as both "$c['one'][2]" and "$a" is at same pointer.
/* Look at memory
 _________________________________
|pointer | value |       variable's           |
 --------------------------------------
|   1     |  NULL  |       ---                  | 
|   2     |  NULL  |       ---                  |  //value of  $b is destroyed and pointer is free
|   3     |  1       |      $c['one'][0]       |
|   4     |  2       |      $c['one'][1]       |
|   5     |  3       |$c['one'][2]  ,$a , $b |  // $b is now here
---------------------------------------
next do */
unset($c['one'][2]);
/* Look at memory
 _________________________________
|pointer | value |       variable's           |
 --------------------------------------
|   1     |  NULL  |       ---                  | 
|   2     |  NULL  |       ---                  | 
|   3     |  1       |      $c['one'][0]       |
|   4     |  2       |      $c['one'][1]       |
|   5     |  3       |      $a , $b              | // $c['one'][2]  is  destroyed not in memory, not in array
---------------------------------------
next do   */
$c['one'][2]=500;    //now it is in array
/* Look at memory
 _________________________________
|pointer | value |       variable's           |
 --------------------------------------
|   1     |  500    |      $c['one'][2]       |  //created it lands on any(next) free pointer in memory
|   2     |  NULL  |       ---                  | 
|   3     |  1       |      $c['one'][0]       |
|   4     |  2       |      $c['one'][1]       |
|   5     |  3       |      $a , $b              | //this pointer is in use
---------------------------------------
lets tray to return $c['one'][2] at old pointer an remove reference $a,$b.  */
$c['one'][2]=&$a;
unset(
$a);
unset(
$b);  
/* look at memory
 _________________________________
|pointer | value |       variable's           |
 --------------------------------------
|   1     |  NULL  |       ---                  | 
|   2     |  NULL  |       ---                  | 
|   3     |  1       |      $c['one'][0]       |
|   4     |  2       |      $c['one'][1]       |
|   5     |  3       |      $c['one'][2]       | //$c['one'][2] is returned, $a,$b is destroyed
--------------------------------------- ?>
I hope this helps.

sony-santos at bol dot com dot br (05-Feb-2007 11:56)

<?php
//if you do:

$a = "hihaha";
$b = &$a;
$c = "eita";
$b = $c;
echo
$a; // shows "eita"

$a = "hihaha";
$b = &$a;
$c = "eita";
$b = &$c;
echo
$a; // shows "hihaha"

$a = "hihaha";
$b = &$a;
$b = null;
echo
$a; // shows nothing (both are set to null)

$a = "hihaha";
$b = &$a;
unset(
$b);
echo
$a; // shows "hihaha"

$a = "hihaha";
$b = &$a;
$c = "eita";
$a = $c;
echo
$b; // shows "eita"

$a = "hihaha";
$b = &$a;
$c = "eita";
$a = &$c;
echo
$b; // shows "hihaha"

$a = "hihaha";
$b = &$a;
$a = null;
echo
$b; // shows nothing (both are set to null)

$a = "hihaha";
$b = &$a;
unset(
$a);
echo
$b; // shows "hihaha"
?>

I tested each case individually on PHP 4.3.10.

martin (05-Jan-2007 12:25)

note that in the previous example all variables (or the one data item all variables point to) is set to NULL, what is interpreted as !isset(), but the linkage between the variables still exists, so

<?php
echo (isset($a)?"set":"unset")."\n";
$a=1;
$b =& $a;
echo (isset(
$b)?"set":"unset")."\n";
$a=null;
echo (isset(
$b)?"set":"unset")."\n";
$a=1;
echo (isset(
$b)?"set":"unset")."\n";
?>

shows:
unset
set
unset
set

note that $b ist set again.

So if you want to brake the linkage, you have to use unset()

lazer_erazer (06-Sep-2006 12:02)

Your idea about unsetting all referenced variables at once is right,
just a tiny note that you changed NULL with unset()...
again, unset affects only one name and NULL affects the data,
which is kept by all the three names...

<?php
$a
= 1;
$b =& $a;
$b = NULL;
?>

This does also work!

<?php
$a
= 1;
$b =& $a;
$c =& $b;
$b = NULL;
?>

donny at semeleer dot nl (13-Jul-2006 03:10)

Here's an example of unsetting a reference without losing an ealier set reference

<?php
$foo
= 'Bob';              // Assign the value 'Bob' to $foo
$bar = &$foo;              // Reference $foo via $bar.
$bar = "My name is $bar"// Alter $bar...
echo $bar;
echo
$foo;                 // $foo is altered too.
$foo = "I am Frank";       // Alter $foo and $bar because of the reference
echo $bar;                 // output: I am Frank
echo $foo;                 // output: I am Frank

$foobar = &$bar;           // create a new reference between $foobar and $bar
$foobar = "hello $foobar"; // alter $foobar and with that $bar and $foo
echo $foobar;              //output : hello I am Frank
unset($bar);               // unset $bar and destroy the reference
$bar = "dude!";            // assign $bar
/* even though the reference between $bar and $foo is destroyed, and also the
reference between $bar and $foobar is destroyed, there is still a reference
between $foo and $foobar. */
echo $foo;                 // output : hello I am Frank
echo $bar;                 // output : due!
?>

libi (24-Jan-2006 08: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 08: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.