473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 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
2 3493
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ravi | last post by:
My XML looks like: <abc> <def type="apple"> 1 </def> <def type="peach"> 2 </def> <def type="orange"> 3 </def> <def type="banana"> 4 </def> <def type="plum"> 5 </def> </abc>
10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
13
by: Benjamin Smith | last post by:
I am controlling the display status of a table row using the following code. <TR id="CCRow" style="DISPLAY:none"> Instead of hard coding "none" above, I would like to change that value using a...
1
by: Peter Herath | last post by:
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want...
4
by: ArrK | last post by:
I want to use a control frame to create and/or modify the content (images and text) of a display frame - all files and images are client side. . When I try to write to the display frame from the...
9
by: tanyali | last post by:
How to set up the environment variable DISPLAY in Linux : 1- Redhat 2- SuSe I used : export DISPLAY=donner:0.0 donner is the domain name. it said : you must specify the...
0
parshupooja
by: parshupooja | last post by:
Contact Reply 1 point Member propoo Joined on 08-31-2007, 10:32 PM Posts 3 Hey all ,
1
by: lrheeza | last post by:
Hello, More questions from this newbie: I have an Application table which has Application ID and Application Name. I have another table Variable which has Variable ID, Application ID and other...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.