21

How check memory location of variable in php?

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
Ben
  • 25,389
  • 34
  • 109
  • 165
  • 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
  • 9
    Your design is broken when you need that. – Gordon Mar 01 '11 at 11:07
  • 1
    related 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 Answers6

12

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
6

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;
}
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
6

If you need to know if a variable is a reference to another, then debug_zval_dump() is the only option.

mario
  • 144,265
  • 20
  • 237
  • 291
4

Sorry, but this is not possible [1]

They are not like C pointers; for instance, you cannot perform pointer arithmetic using them

[1] http://php.net/manual/en/language.references.php

3

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;
?>
dAm2K
  • 9,923
  • 5
  • 44
  • 47
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
2

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.)

Community
  • 1
  • 1
jameshfisher
  • 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