How do I display the name of a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    How do I display the name of a variable?


    I have a script (below) that can be passed an array and it will dump the
    contents of the array in to an html table - I use it during development so
    its nothing sexy. It handles multidimsional arrays so any element that
    itself is an array will also be broken down and displayed...

    Thus... in order to keep track of tables in a multidimensiona l array, I'm
    trying to print the name of each array before its displayed... Does anybody
    know a method how I could do this?

    Example: If I were to call the script with dumpArray($_SER VER) it would dump
    out the contents of the array $_SERVER. How could I have my output titled
    (for example) "Contents of _SERVER array"... since I can pass it many
    different arrays, it needs to be dynamic and not hard coded... I could be
    passing it any array with any name, and multi-dimensional arrays could
    contain other arrays...

    All help appreciated - script follows below...

    function dumpArray($arra yName)
    {
    // Dump an array to the client in an html table format
    // Useful for debugging
    print("<BR><TAB LE BORDER=1>");
    if(is_array($ar rayName))
    { $telements=coun t($arrayName);
    print("<th align=right>#</th><th>$telemen ts Elements</th><th
    align=right>str len</th><tr>");
    foreach($arrayN ame as $key=>$value)
    { if(is_array($va lue))
    { print("<TD COLSPAN=3>Eleme nt $key</TD><TR>");
    dumpArray($valu e); }
    else
    { $length=strlen( $value);
    if($length==0)
    { $value="&nbsp;" ; }
    else
    { $value=htmlenti ties($value); }
    print("<TH>$key </TH><TD>$value</TD><td>$length</td><TR>");
    }
    }
    }
    else
    { print("<TH>Arra y Not Defined or Empty - Cannot Dump</TH><TR>"); }

    print("</TABLE>");
    return;
    }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet?


  • Andy Hassall

    #2
    Re: How do I display the name of a variable?

    On Tue, 23 Sep 2003 21:41:46 GMT, "Randell D."
    <you.can.email. me.at.randelld@ yahoo.com> wrote:[color=blue]
    >
    >I have a script (below) that can be passed an array and it will dump the
    >contents of the array in to an html table - I use it during development so
    >its nothing sexy. It handles multidimsional arrays so any element that
    >itself is an array will also be broken down and displayed...
    >
    >Thus... in order to keep track of tables in a multidimensiona l array, I'm
    >trying to print the name of each array before its displayed... Does anybody
    >know a method how I could do this?
    >
    >Example: If I were to call the script with dumpArray($_SER VER) it would dump
    >out the contents of the array $_SERVER. How could I have my output titled
    >(for example) "Contents of _SERVER array"...[/color]

    In short; you can't. In your example it's passed by value, and even if it were
    passed by reference, it doesn't matter; since the 'variable name' is always
    $arrayName. Variable names don't get passed around; only the data they
    represent does.
    [color=blue]
    >since I can pass it many
    >different arrays, it needs to be dynamic and not hard coded... I could be
    >passing it any array with any name, and multi-dimensional arrays could
    >contain other arrays...[/color]

    The contents of multidimensiona l arrays will have the keys to print as the
    'name', so that's not a problem, but you're not going to be able to get hold of
    the top-level variable name with the way you're calling it.

    You _could_ pass a variable name, not the variable itself, but that would only
    work with global variables. Any local variables would be out of scope within
    the function call, so you wouldn't see them.

    If PHP had macros, you could define one that changed dumpArray(x) into
    dumpArray($x, 'x') - but unless they've sneaked macros in since I last looked,
    you can't. So redefining your function take a name would be about the only way.

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Randell D.

      #3
      Re: How do I display the name of a variable?


      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:ogh1nvopf4 tej0h6em0l88p6a 6t20bq529@4ax.c om...[color=blue]
      > On Tue, 23 Sep 2003 21:41:46 GMT, "Randell D."
      > <you.can.email. me.at.randelld@ yahoo.com> wrote:[color=green]
      > >
      > >I have a script (below) that can be passed an array and it will dump the
      > >contents of the array in to an html table - I use it during development[/color][/color]
      so[color=blue][color=green]
      > >its nothing sexy. It handles multidimsional arrays so any element that
      > >itself is an array will also be broken down and displayed...
      > >
      > >Thus... in order to keep track of tables in a multidimensiona l array, I'm
      > >trying to print the name of each array before its displayed... Does[/color][/color]
      anybody[color=blue][color=green]
      > >know a method how I could do this?
      > >
      > >Example: If I were to call the script with dumpArray($_SER VER) it would[/color][/color]
      dump[color=blue][color=green]
      > >out the contents of the array $_SERVER. How could I have my output[/color][/color]
      titled[color=blue][color=green]
      > >(for example) "Contents of _SERVER array"...[/color]
      >
      > In short; you can't. In your example it's passed by value, and even if it[/color]
      were[color=blue]
      > passed by reference, it doesn't matter; since the 'variable name' is[/color]
      always[color=blue]
      > $arrayName. Variable names don't get passed around; only the data they
      > represent does.
      >[color=green]
      > >since I can pass it many
      > >different arrays, it needs to be dynamic and not hard coded... I could be
      > >passing it any array with any name, and multi-dimensional arrays could
      > >contain other arrays...[/color]
      >
      > The contents of multidimensiona l arrays will have the keys to print as[/color]
      the[color=blue]
      > 'name', so that's not a problem, but you're not going to be able to get[/color]
      hold of[color=blue]
      > the top-level variable name with the way you're calling it.
      >
      > You _could_ pass a variable name, not the variable itself, but that would[/color]
      only[color=blue]
      > work with global variables. Any local variables would be out of scope[/color]
      within[color=blue]
      > the function call, so you wouldn't see them.
      >
      > If PHP had macros, you could define one that changed dumpArray(x) into
      > dumpArray($x, 'x') - but unless they've sneaked macros in since I last[/color]
      looked,[color=blue]
      > you can't. So redefining your function take a name would be about the only[/color]
      way.[color=blue]
      >
      > --
      > Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      > Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)[/color]

      Thanks... I had thought there would have been a method possible, but as you
      say, the data, not the variable name is passed around... and I had not
      thought of it that way...

      Cheers
      Randell D.


      Comment

      Working...