note 73663 added to language.references
| From: | sneskid at hotmail dot com | Date: | Tue, 06 Mar 2007 08:47:36 +0000 |
| Subject: | note 73663 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124137@lists.php.net to get a copy of this message | ||
(v5.1.4)
One cool thing about var_dump is it shows which variables are references (when dumping arrays),
symbolized by 'â«' for int/null, and by '&' for
boolean/double/string/array/object. I don't know why the difference in symmmmbolism.
After playing around I found a better way to implement detaching (twas by accident). var_dump can
show what's going on.
<?php
function &detach($v=null){return $v;}
$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.
----
Server IP: 69.147.83.197
Probable Submitter: 74.118.123.241
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/73663
Del: integrated -- https://master.php.net/note/delete/73663/integrated
Del: useless -- https://master.php.net/note/delete/73663/useless
Del: bad code -- https://master.php.net/note/delete/73663/bad+code
Del: spam -- https://master.php.net/note/delete/73663/spam
Del: non-english -- https://master.php.net/note/delete/73663/non-english
Del: in docs -- https://master.php.net/note/delete/73663/in+docs
Del: other reasons-- https://master.php.net/note/delete/73663
Reject -- https://master.php.net/note/reject/73663
Search -- https://master.php.net/manage/user-notes.php