4

Possible Duplicate:
Detecting whether a PHP variable is a reference / referenced

I am wondering if there is a function that will tell me if a variable is a reference variable. If there is not a specific function, is there a way to determine if it is a reference variable?

Community
  • 1
  • 1
Josh Pennington
  • 6,418
  • 13
  • 60
  • 93

4 Answers4

1

You can determine this using debug_zval_dump. See my answer on another question.

Community
  • 1
  • 1
NikiC
  • 100,734
  • 37
  • 191
  • 225
0

From the user examples it looks like there is no direct way, but you'll find a solution there.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

You can try using this function from one of the commenters at PHP docs. But afaik there is not built-in function that would check if var is reference var.

usoban
  • 5,428
  • 28
  • 42
0
<?php
$a = 1;
$b =& $a;
$c = 2;
$d = 3;
$e = array($a);
function is_reference($var){
    $val = $GLOBALS[$var];
    $tmpArray = array();
    /**
     * Add keys/values without reference
     */
    foreach($GLOBALS as $k => $v){
        if(!is_array($v)){
            $tmpArray[$k] = $v;
        }
    }

    /**
     * Change value of rest variables
     */
    foreach($GLOBALS as $k => $v){
        if($k != 'GLOBALS'
            && $k != '_POST'
            && $k != '_GET'
            && $k != '_COOKIE'
            && $k != '_FILES'
            && $k != $var
            && !is_array($v)
        ){
            usleep(1);
            $GLOBALS[$k] = md5(microtime());
        }
    }

    $bool = $val != $GLOBALS[$var];

    /**
     * Restore defaults values
     */
    foreach($tmpArray as $k => $v){
        $GLOBALS[$k] = $v;
    }

    return $bool;
}
var_dump(is_reference('a'));
var_dump(is_reference('b'));
var_dump(is_reference('c'));
var_dump(is_reference('d'));
?>

This is an example from the PHP documentation.

Griffin
  • 644
  • 6
  • 18