PHP: Сброс переменных-ссылок - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Неявное использование механизма ссылок Возвращение по ссылке

Last updated: Sat, 27 Jan 2007

view this page in

# Сброс переменных-ссылок

При сбросе ссылки, просто разрывается связь имени и содержимого переменной. Это не означает, что содержимое переменной будет разрушено. Например:

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

Этот код не сбросит `$b`, а только `$a`.

Опять же, можно провести аналогию с вызовом unlink (в Unix).

add a note User Contributed Notes
Сброс переменных-ссылок
sony-santos at bol dot com dot br
05-Feb-2007 03: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 04: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
05-Sep-2006 04: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 07: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!
?>`
rustin dot phares at hotmail dot com
12-Mar-2006 10: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 12: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 12: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.`
add a note
Неявное использование механизма ссылок Возвращение по ссылке

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST