Voting

: zero plus seven?
(Example: nine)

The Note You're Voting On

sneskid at hotmail dot com
12 years ago
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;
}
?>

<< Back to user notes page

To Top