How check memory location of variable in php?
- 
                    yes I need, for example if I want to know if variable is copy of other variable or pointer to the same variable – Ben Mar 01 '11 at 11:04
- 
                    9Your design is broken when you need that. – Gordon Mar 01 '11 at 11:07
- 
                    1related to if not duplicate of [Detecting whether a variable is a reference](http://stackoverflow.com/questions/4817562/detecting-whether-a-php-variable-is-a-reference-referenced) – Gordon Mar 01 '11 at 11:10
- 
                    1@Gordon to be fair, he might be using PHP in a non-traditional environment where he has to know the memory addresses of his data. he probably didn't design the system... – William Feb 01 '16 at 18:40
6 Answers
If you need to know if $varA is a reference to $varB, then you're out of luck: the PHP innards does not present this information to the developer.
However, you can extract some information about references by parsing the output from var_dump or debug_zval_dump(). Read the relevant manual sections, and see this question for some details.
And have a ready of this (PDF) article by Derick Rethans on references in PHP.
Watch out for the refcount when using debug_zval_dump() because the function always creates an additional reference within itself, incrementing the value by 1
 
    
    - 209,507
- 32
- 346
- 385
To test if one variable is a reference to another you can do the following:
function is_ref_to(&$a, &$b)
{
    $t = $a;
    if($r=($b===($a=1))){ $r = ($b===($a=0)); }
    $a = $t;
    return $r;
}
 
    
    - 9,833
- 3
- 32
- 58
If you need to know if a variable is a reference to another, then debug_zval_dump() is the only option.
 
    
    - 144,265
- 20
- 237
- 291
Sorry, but this is not possible [1]
They are not like C pointers; for instance, you cannot perform pointer arithmetic using them
 
    
    - 884
- 1
- 8
- 19
- 
                    maybe this could help you... =D : http://www.php.net/manual/en/language.references.spot.php#102177 – Felipe Cardoso Martins Mar 01 '11 at 11:10
I'm fairly certain this isn't possible in PHP. You might be able to create an extension to do this, but I can't see any use for it. Memory addresses are useless within PHP scripts since the interpreter handles all the internal variable works. Outside of PHP (say manipulating a memory address with C or C++) would be dangerous to say the least. I would expect you could crash a script and possibly your interpreter if you modified a memory address used by a PHP script while it was executing.
If you are looking for internal pointers in PHP, though, take a look into references. It might take a few reads to wrap your head around them, but if you need to pass by reference, look into them. Quick example:
<?php
function change( &$name ) { $name = "Yosef"; }
$name = "John";
change( $name );
echo $name;
?>
 
    
    - 9,923
- 5
- 44
- 47
 
    
    - 15,599
- 5
- 53
- 91
As others have said, the API does not expose a way to retrieve a value's address. However, you can compare the addresses of two values, given two variables. Here's a function that tests whether two variables reference the same value:
function vars_reference_same_value(&$a, &$b)
{
  $not_b = $b === 0 ? 1 : 0;
  $a_orig = $a;
  $a = $not_b;
  $is_ref = $b === $not_b;
  $a = $a_orig;
  return $is_ref;
}
(This is the same as @infinity's answer, but perhaps more comprehensible.)
 
    
    - 1
- 1
 
    
    - 34,029
- 31
- 121
- 167
- 
                    Is this better than the far more detailed code (that looks to do the same thing) at http://www.php.net/manual/en/language.references.spot.php#72203 ? – artfulrobot Feb 03 '14 at 11:51
 
    