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?

4
  • I don't think there's a built-in function to check if a variable is a reference. Commented Oct 20, 2011 at 14:29
  • Interesting question, but I wonder what the use case is. Commented Oct 20, 2011 at 14:30
  • The use case was for debugging a variable that was changing unexpectedly. Commented Oct 20, 2011 at 14:43
  • If it's for debugging, just var_dump it. Commented Oct 20, 2011 at 15:09

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

If you've answered this on another question, why did you answer it here?
0

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

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.