By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

How do I display the name of a variable?

P: n/a

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 multidimensional 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($_SERVER) 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($arrayName)
{
// Dump an array to the client in an html table format
// Useful for debugging
print("<BR><TABLE BORDER=1>");
if(is_array($arrayName))
{ $telements=count($arrayName);
print("<th align=right>#</th><th>$telements Elements</th><th
align=right>strlen</th><tr>");
foreach($arrayName as $key=>$value)
{ if(is_array($value))
{ print("<TD COLSPAN=3>Element $key</TD><TR>");
dumpArray($value); }
else
{ $length=strlen($value);
if($length==0)
{ $value="&nbsp;"; }
else
{ $value=htmlentities($value); }
print("<TH>$key</TH><TD>$value</TD><td>$length</td><TR>");
}
}
}
else
{ print("<TH>Array 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?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Tue, 23 Sep 2003 21:41:46 GMT, "Randell D."
<yo**************************@yahoo.com> wrote:

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 multidimensional 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($_SERVER) it would dump
out the contents of the array $_SERVER. How could I have my output titled
(for example) "Contents of _SERVER array"...
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.
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...


The contents of multidimensional 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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2

P: n/a

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:og********************************@4ax.com...
On Tue, 23 Sep 2003 21:41:46 GMT, "Randell D."
<yo**************************@yahoo.com> wrote:

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 soits 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 multidimensional array, I'm
trying to print the name of each array before its displayed... Does anybodyknow a method how I could do this?

Example: If I were to call the script with dumpArray($_SERVER) it would dumpout the contents of the array $_SERVER. How could I have my output titled(for example) "Contents of _SERVER array"...
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.
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...
The contents of multidimensional 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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)


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.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.