getting variable name from the variable itself? 
Author Message
 getting variable name from the variable itself?

Yes, it is mainly for debugging purposes. I want a clean way that dumps
variables in a nicer and more readable fashion than print_r does.
And then it's a bit of pure curiosity as well. Just wanted to know if it was
possible at all.

Well, I guess it can't be done then.

Thanks for your input everyone.
/Ludde



Quote:


> >>... but the varname isn't printed neither.

> > Which suggests that even the php guys don't know how to do that ;)

> Actually I hope they do ;). The point is, IMO, that you don't need it in
> normal applications. Only for debugging purposes. But writing:

> <?php
> $var = 'foo';
> dumpvar($foo); // the function from the OP
> ?>

> or

> <?php
> $var = 'foo';
> echo 'foo: ';
> var_dump($foo) // or print_r
> ?>

> doesn't make much of a difference. If you need such a function for
> non-debugging purposes, then you have a mistake in your design IMHO.

> greetings

> Carsten

> --
> Carsten Saathoff          ...powered by Linux              ICQ #52558095

> Abhor common norms, life's yours to form.
>                       <Ebony Tears>



Mon, 14 Mar 2005 18:18:26 GMT  
 getting variable name from the variable itself?
You are talking about variable variables, getting a variable from a string.
I want it the opposite way, getting a string from a variable.
But have been more or less convinced that it can't be done.

/Ludde



Quote:


> > >... but the varname isn't printed neither.

> > Which suggests that even the php guys don't know how to do that ;)

> I'm not sure if I'm following this right, but here's how I used
> variable variable names for my application (online real estate
> listings).  I have a page that presents a form allowing the user to
> upload a PDF brochure by selecting up to eight PDF files (pages 1 - 8)
> and then clicking on upload.  So, I have a do loop in PHP that spits
> out the input form fields using variable variable names thusly:

> <?
> // this generates eight input fields named page1 - page8

> $page_num = 1;

> while ($page_num <= $MAX_PDF_PAGES)  //defined within PHP file as 8
> {

>    $input_name = "page" . $page_num;

> ?>
>          <input type=file size=48 name=<?= $input_name ?>
>             accept="image/gif,jpg,jpeg" value="">
> <?

>    ++page_num;
> }

> ?>

> The form action invokes another php file that loops through the inputs
> to see which ones were specified - which had files uploaded to them.
> This is still in the test stage, so it prints out the information, ...
> like so:

> $page_num = 1;

> while ($page_num <= $MAX_PDF_PAGES) // = 8
> {
>    $input_name = "page" . $page_num;

>    print "$input_name = ";
>    print "${$input_name} <br>";   // variable variable name

>    // check to see if this page was uploaded - note the "varibale
> variable" name
>    if (isset(${$input_name}) && ${$input_name} != "none")
>    {

>       $pdf_fname = $page_num . ".pdf";

>       $fpath = '../pdfs/' . $pdf_fname;

>       //move uploaded file
>       if (!move_uploaded_file($HTTP_POST_FILES[$input_name][tmp_name],

>          "$fpath"))
>       {
>          echo "Possible file upload attack. Filename: ${$input_name}";

>          die();
>       }
>    }
>    ++$page_num;
> }

> --
> **********************************************
>  Chuck Anderson . Boulder, CO    CycleTourist at
>  http://www.CycleTourist.com           attbi.com
>  Tolerance is recognizing that other people have different ideals
>  and needs than you. Compromise is acting on that knowledge.
> ***********************************************************



Mon, 14 Mar 2005 18:21:36 GMT  
 getting variable name from the variable itself?

Quote:

> Is there any way to get the name from a variable?

I've posted the same question several week ago, but didn't get better
result than you :-(

I was tring to write a function that could "juggle" a little with
several variables from _POST, session, and sundryes. I had found what I
thought was a nice solution, but required extracting the name from a
variable, i.e. the "reverse" of a variable variable...

I ended up doing what I was trying to do by accessing var directly as
$GLOBALS["varname"] rather than $varname. This could help you?

bye!

--
Fabrizio "Hermooz" Ermini



Mon, 14 Mar 2005 21:30:47 GMT  
 getting variable name from the variable itself?

Quote:


>>Is there any way to get the name from a variable?
>>Let's say I have this variable $badger that is set to "Mr Jones" and I want
>>to print it with a label. Like this: "Badger: Mr Jones"
>>I would like to be able to do something like this:

>>echo variable_name($badger).": ".$badger;

> Why? I don't see the point here. Why don't you just write:

> echo 'badger: ' . $badger;

> The variable names won't change after you wrote the script, so why do
> you need the function then?

>>I know I can do something like this

>>function printvar($var) {
>>    echo $var.": ".$$var;
>>}
>>printvar("badger");

> In this case the variable names are dynamic, but the name is $var
> nevertheless, so you again don't need the function .

>>There is a subtle difference and I don't know if there is ever a place where
>>you can't use the second method,
>>but I would just like to know if it's possible to get the actual name
>>anyway.
>>I have searched the manual without finding anything.

> Yes, because such a funtion would make no sense.

> hth

> Carsten

--
Fabrizio "Hermooz" Ermini (FI)
Su BMW F 650 GS "Kandarino"


Mon, 14 Mar 2005 21:31:52 GMT  
 getting variable name from the variable itself?

Quote:

> Why? I don't see the point here. Why don't you just write:

> echo 'badger: ' . $badger;

> The variable names won't change after you wrote the script, so why do
> you need the function then?

Well, this is not entirely true. At least if you work with
register_globals on, you can have names of the variables that are the
names of the INPUT fields of the form originating the request; if you
are making dynamic Forms (a widespread technique, I think) you then must
find a way to deal with dynamic variable space... OK, maybe this has
nothing to do with the problem at hand, but just to be precise ;-)

bye!

--
Fabrizio "Hermooz" Ermini



Mon, 14 Mar 2005 21:37:04 GMT  
 getting variable name from the variable itself?



Quote:
> I ended up doing what I was trying to do by accessing var directly as
> $GLOBALS["varname"] rather than $varname. This could help you?

But that's the same as a variable variable.
$GLOBALS["varname"] is the same as ${"varname"}.

/Ludde



Mon, 14 Mar 2005 22:29:47 GMT  
 getting variable name from the variable itself?

Quote:



>>I ended up doing what I was trying to do by accessing var directly as
>>$GLOBALS["varname"] rather than $varname. This could help you?

> But that's the same as a variable variable.
> $GLOBALS["varname"] is the same as ${"varname"}.

Well, sort of. As I said, at the end I flipped the logic and found a way
to do what I intended to getting variable names using that notation
instead.
It is NOT an answer to that problem; is just an example of a work around...

bye!

--
Fabrizio "Hermooz" Ermini



Mon, 14 Mar 2005 23:02:46 GMT  
 getting variable name from the variable itself?

Quote:


>> Why? I don't see the point here. Why don't you just write:

>> echo 'badger: ' . $badger;

>> The variable names won't change after you wrote the script, so why do
>> you need the function then?

> Well, this is not entirely true. At least if you work with
> register_globals on, you can have names of the variables that are the
> names of the INPUT fields of the form originating the request; if you
> are making dynamic Forms (a widespread technique, I think) you then must
> find a way to deal with dynamic variable space... OK, maybe this has
> nothing to do with the problem at hand, but just to be precise ;-)

1) register_globals is bad! ;)
2) There are much better ways to program dynamic forms. For example:
<input type="text name="foo[]">
will give you an array in PHP. When you use either the superglobals or
the old $HTTP_*_VARS you don't have the problem at all.

So I stay with my thesis: You don't need a function, that prints the
variable name ;).

greetings

Carsten

--
Carsten Saathoff          ...powered by Linux              ICQ #52558095

                 Abhor common norms, life's yours to form.
                             <Ebony Tears>



Mon, 14 Mar 2005 23:31:01 GMT  
 getting variable name from the variable itself?

Quote:

> 1) register_globals is bad! ;)

No! It is not! :-))))

Quote:
> So I stay with my thesis: You don't need a function, that prints the
> variable name ;).

Pleeeeaaaaase... I promise I'll be a good child if you give me my new
function :-))

Just kidding, Carsten... bye!

--
Fabrizio "Hermooz" Ermini



Mon, 14 Mar 2005 23:46:59 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Using a variable name for a variable name??

2. Getting user-variable/equate name at runtime

3. Getting name of use-variable of a screencontrol

4. Getting the name of a variable

5. Getting variable name

6. getting the name of a variable

7. variable contents to variable name

8. array name in variable variable

9. Variables containing variable names

10. name of a variable stored in another variable

11. Variable variable names?

12. values of variable variable-names

 

 
Powered by phpBB® Forum Software