NYCPHP Meetup

[nycphp-talk] Output name of Variable

Jim Musil jim at nettmedia.com
Mon Feb 10 12:24:37 EST 2003


Yes, you are right, I would already know the name, but I thought it was
silly that I had to enter it twice.

For instance, I have a function similar to this ...

    function bug($var,$label) {

        echo " $label<pre>";
        print_r($var);
        echo "</pre>";

    }

... And I use it liberally to trace variables and debug:

As in 
    
    if($DEBUG) bug($foo,"THIS IS FOO AND IT SHOULD EQUAL ONE");

But, I thought it was silly that I had to write the name twice, because
sometimes it's obvious what the variable is...


    bug($number_of_rows);


And I wanted to have my output be

    $number_of_rows: 5


The closest solution is to send the variable name without the $.

    function bug($var) {

        global $$var;

        echo "$".$var;
    
        echo "<pre>";
        print_r($$var);
        echo "</pre>";

    }

    bug("foo");

To me, this is kind of silly, too.

It just seems like there should be a simple function to do this. It should
be part of print_r(), which already will say what type of object it is and
will spell out the names of the object variables and associative array keys.

How hard would it have been for them to print out the name of the variable?


Jim Musil
<jim at nettmedia.com>
Nettmedia, Senior Developer
345 Seventh Ave., 24th Floor.
New York, NY 10001
Tel. 212.629.0004 x 131


On 2/7/03 8:18 PM, "Analysis & Solutions" <danielc at analysisandsolutions.com>
wrote:

> Hi:
> 
> On Fri, Jan 31, 2003 at 12:37:31PM -0500, Jim Musil 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?
> 
> While I understand what you want to do, I don't quite understand what
> you're getting at.
> 
> In order to find the name of the variable in question, you need to know
> the name of the variable in the first place in order to ask the question
> about it.
> 
> Kind of an existential situation, no? :)
> 
> For example, in order to perform the action of unsetting a variable via
> unset(), you need to know the name of the variable:  say unset($foo).
> 
> Similarly, if you wanted to do a function like whats_my_name(), you'd
> similarly have to put the name of the variable in there.  So, you already
> know the name.
> 
> What am I missing here?
> 
> Are you using varibable variables and trying to find the name of the
> initial variable?
> 
> See you,
> 
> --Dan




More information about the talk mailing list