note 107946 added to language.references
| From: | sneskid at hotmail dot com | Date: | Thu, 15 Mar 2012 22:19:19 +0000 |
| Subject: | note 107946 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186697@lists.php.net to get a copy of this message | ||
There is no built in method (yet) to check if two variables are references to the same piece of
data, but you can do a "reference sniff" test. This is rarely needed, but can be very
useful. The function bellow is a slightly modified version of this technique I saw in a forum
regarding this comparison limitation.
<?php
function is_ref_to(&$a, &$b)
{
$t = $a;
if($r=($b===($a=1))){ $r = ($b===($a=0)); }
$a = $t;
return $r;
}
$varA = 1;
$varB = $varA;
$varC =&$varA;
var_dump( is_ref_to($varA, $varB) ); // bool(false)
var_dump( is_ref_to($varA, $varC) ); // bool(true)
?>
The test above uses a two step process to be 100% generic.
But if you are sure the variables being tested will not be a certain value, example null, then use
that value to allow a one step check.
<?php
function is_ref_to_1step(&$a, &$b)
{
$t = $a;
$r=($b===($a=null));
$a = $t;
return $r;
}
?>
----
Server IP: 72.55.140.6
Probable Submitter: 69.159.196.196
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/107946
Del: integrated -- https://master.php.net/note/delete/107946/integrated
Del: useless -- https://master.php.net/note/delete/107946/useless
Del: bad code -- https://master.php.net/note/delete/107946/bad+code
Del: spam -- https://master.php.net/note/delete/107946/spam
Del: non-english -- https://master.php.net/note/delete/107946/non-english
Del: in docs -- https://master.php.net/note/delete/107946/in+docs
Del: other reasons-- https://master.php.net/note/delete/107946
Reject -- https://master.php.net/note/reject/107946
Search -- https://master.php.net/manage/user-notes.php