$A=array('x' => 123, 'y' => 321); $A['x'] = &$A['x']; var_dump($A); /* x became it's own reference...
array(2) {
["x"]=> ∫(123)
["y"]=> int(321)
}*/
$A['y']=&$A['x']; var_dump($A); /* now both are references
array(2) {
["x"]=> ∫(123)
["y"]=> ∫(123)
}*/
$z = 'hi'; $A['y']=&detach(&$z); var_dump($A); /* x is still a reference, y and z share
array(2) {
["x"]=> ∫(123)
["y"]=> &string(2) "hi"
}*/
$A['x'] = $A['x']; $A['y']=&detach(); var_dump($A,$z); /* x returned to normal, y is on its own, z is still "hi"
array(2) {
["x"]=> int(123)
["y"]=> NULL
}*/ ?>
For detach to work you need to use '&' in the function declaration, and every time you call it.
Use this when you know a variable is a reference, and you want to assign a new value without effecting other vars referencing that piece of memory. You can initialize it with a new constant value, or variable, or new reference all in once step.
sneskid at hotmail dot com (01-Nov-2006 01:33)
in addition to what 'jw at jwscripts dot com' wrote about unset; it can also be used to "detach" the variable alias so that it may work on a unique piece of memory again.