Results 1 to 7 of 7

Thread: Get_Variable_Name

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    Get_Variable_Name

    This is probably dumb but.. how can I find out the name of a variable? What I want is this:

    Code:
    $var = 'bla';
    $foo = array( 'yeah' );
    
    print_name( $var ); // Print: var
    print_name( $foo ); // Print: foo

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Get_Variable_Name

    echo ('foo'); -- Prints the name of your variable. You already know the name of the variable and the name of a variable does not change, therefore there is no need find it at runtime.

    What is it you are trying to do? - maybe there is another way around your problem.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    therefore there is no need find it at runtime.
    Sure there is... see this function:

    Code:
    function print_field( $var )
    {
        print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
    }
    So I'm missing varname() here. I know that I could pass another parameter with the name of this variable but I'm trying to simplify things as well as possible. There must be a way to get the name of a referenced variable!

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    try this.. I just whipped it up.

    PHP Code:
    <?
      /*
       * Author: David Miles - [email][email protected][/email]
       * function varname([str varname])
       * Returns the value of varname and inserts it into an HTML <input>
       * Usage: varname("var") returns the value of $var
      */
      function varname($str){
        global ${$str};
        return "<input name='$str' value='" . ${$str} . "'>";
      }
    ?>
    I'd appreciate it if you left my author tag in there if you use it..

    I threw in your <INPUT> code just for ease.. call it like:
    PHP Code:
    <?  $thestr = "value";
      echo "<xmp>" . varname("thestr") . "</xmp>";
    ?>
    and it will return
    Code:
    <input name='thestr' value='value'>
    Hope it can helps you out..

    edit: It sort of does the opposite of what you want.. but it should work for what you're doing..
    Last edited by kows; Mar 21st, 2004 at 08:12 PM.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Originally posted by Fox
    Sure there is... see this function:

    Code:
    function print_field( $var )
    {
        print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
    }
    So I'm missing varname() here. I know that I could pass another parameter with the name of this variable but I'm trying to simplify things as well as possible. There must be a way to get the name of a referenced variable!
    kows - solution is best. You'll need to pass the variable name as a string.

    When you pass a variable to a function copy is made and the local variable comes into scope with the name you gave it in the function. So even if you could determine the name of the variable - it would be the name of the local function variable returned and not the global one.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    yeah, exactly. I thought it'd be fairly easy, but found out that wasn't so when I stuck the variable into a temporary array as $tmp[$str] and then called a foreach($tmp as $key => $val).. it seemed like it'd work, and would've worked fine if it wasn't in a function. It kept returning the name of the variable that I defined in the function's parameters. Anyway, PHP doesn't recognize the fact that you passed a variable into your function.. it just stuck whatever the value of it was into the variable you referenced within the parameters and then goes on with executing the function. I don't think there's any way to grab the variable's name that's being referenced.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Hm that's right the referencing causes troubles...

    Well guess I'll just leave it as it is now, that's passing the name as a string.

    Thanks anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width