How check memory location of variable in php?
Thanks
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
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;
?>
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;
}
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
If you need to know if a variable is a reference to another, then debug_zval_dump() is the only option.
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.)