search for in the  
<Returning ReferencesSpotting References>
Last updated: Thu, 19 May 2005

Unsetting References

When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example:

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

won't unset $b, just $a.

Again, it might be useful to think about this as analogous to Unix unlink call.



User Contributed Notes
Unsetting References
mangelp at ieee dot org
15-Dec-2004 06:24
Bishop: Sorry but you are making a mistake.
When you do this:
<?php
$orig
= 1;
$ref  = &$orig;
?>

You have two variables that point to the same content. So if you unset one of them you are only erasing one of the references (take a look at the documentation above) not erasing the content itself. The content is erased when it is no more references left.

If what you wanted was to delete completely the content, not the references to the content, you should do what you proposed setting to null the content.
This will make that every reference acts like there wheren't set to any value, but this is only a fake because the content is the null value.
bishop
19-Apr-2004 09:31
The unset() behaviour on a referenced variable is very strange and quite counter-intuitive. Consider:

<?php
$orig
= 1;
$ref  = &$orig;

// ref is &int(1)
unset($orig);
// ref is int(1)
// NOTE the reference is gone, but the content remains
?>

A small workaround is to null your variables, then to unset them. Eg:

<?php
$orig
= null;
unset(
$orig);
?>

This will at least prevent the original value from propogating to the uncoupled reference.

<Returning ReferencesSpotting References>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT