NYCPHP Meetup

NYPHP.org

[nycphp-talk] Output name of Variable

gadhra bfeqx44002 at sneakemail.com
Fri Jan 31 14:14:28 EST 2003


This is kind of a different tack - I don't know if it could help you or 
not.  It uses a function call, so you have to be careful of the variable 
scope.

Function is something like:

function show_variable($arr,$var) {
    foreach ($arr as $key=>$val) {
        if ($key == $var) {
            $str = "$$key = $val";
            return $str;
        }
    }
}

/* Then, you can call it like this: */
$test = "Test me!";
echo show_variable(get_defined_vars(),"test");
/*  result is $test = Test me!  */

Since get_defined_vars() has all the variables in your current scope, it 
needs to be passed to the function.   The need to pass the name of the 
variable in quotes might be might be suboptimal for you, but I can 
confirm that this works, kicking out the appropriate string.

-- 
+gadhra+(/s)


Jim Musil jim-at-nettmedia.com |NY PHP| wrote:

>It seems like it would be helpful if there were a way to grab the name
>assigned to a variable and output it. Is there already a function like this?
>
>I would use this mainly for tracing and reporting. So, if I had a simple
>function show_variable($var), it would not only output the value assigned to
>the variable, but would also show the name of the variable itself.
>
>Am I missing it?
>
>
>Jim Musil
><jim at nettmedia.com>
>Nettmedia, Senior Developer
>345 Seventh Ave., 24th Floor.
>New York, NY 10001
>Tel. 212.629.0004 x 131
>
>
>
>--- Unsubscribe at http://nyphp.org/list/ ---
>
>
>
>
>
>  
>




More information about the talk mailing list