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

Echo the actual Variable name

P: n/a
So i sit here thinking, and wondering...

say for example i have a variable called $definition_london

Say for exaple that has not set value...

Is it possible to echoe thew ACTUAL variable name into a document, rather
than the variables information?

Thanks for yout time, look forward to all replies :)
Feb 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Dawsons" <st****@3branch.co.uk> wrote in message
news:Aa*******************@newsfe4-win.ntli.net...
So i sit here thinking, and wondering...

say for example i have a variable called $definition_london

Say for exaple that has not set value...

Is it possible to echoe thew ACTUAL variable name into a document, rather
than the variables information?

Thanks for yout time, look forward to all replies :)

I'm no sure what you are exactly looking for, but you could do something
like this:

echo 'Here is a list of all variables and their values defined in this
script:<br />';
print_r(get_defined_vars());

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 20 '06 #2

P: n/a
echo '$definition_london is in a string';

Feb 20 '06 #3

P: n/a
Dawsons wrote:
Is it possible to echoe thew ACTUAL variable name into a document


function getvarname(&$var)
{
$ret = '';
$tmp = $var;
$var = md5(uniqid(rand(), TRUE));

$key = array_keys($GLOBALS);
foreach ( $key as $k )
if ( $GLOBALS[$k] === $var )
{
$ret = $k;
break;
}

$var = $tmp;
return $ret;
}

$another = 'test';
$testvar = 'test';
echo getvarname($testvar); //echoes 'testvar'

--
E. Dronkert
Feb 20 '06 #4

P: n/a
Thank you very much, this is excellent!

"Ewoud Dronkert" <fi*******@lastname.net.invalid> wrote in message
news:dd********************************@4ax.com...
Dawsons wrote:
Is it possible to echoe thew ACTUAL variable name into a document


function getvarname(&$var)
{
$ret = '';
$tmp = $var;
$var = md5(uniqid(rand(), TRUE));

$key = array_keys($GLOBALS);
foreach ( $key as $k )
if ( $GLOBALS[$k] === $var )
{
$ret = $k;
break;
}

$var = $tmp;
return $ret;
}

$another = 'test';
$testvar = 'test';
echo getvarname($testvar); //echoes 'testvar'

--
E. Dronkert

Feb 20 '06 #5

P: n/a
Dawsons said the following on 20/02/2006 10:51:
So i sit here thinking, and wondering...

say for example i have a variable called $definition_london

Say for exaple that has not set value...

Is it possible to echoe thew ACTUAL variable name into a document, rather
than the variables information?


Maybe I'm missing something, but why can't you just do echo
'$definition_london'; ??

--
Oli
Feb 20 '06 #6

P: n/a
Dawsons wrote:
Thank you very much, this is excellent!


Only works for globals though.

--
E. Dronkert
Feb 20 '06 #7

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dawsons wrote:
say for example i have a variable called $definition_london

Say for exaple that has not set value...
If it has no value, that varable name does not exist. Bye-bye.
Is it possible to echoe thew ACTUAL variable name into a document, rather
than the variables information?


No, because a variable can have more than one, due to the way PHP handles
references. Suppose the following code:

<?php

$a = 42;

$b =& $a;
$c =& $a;
unset ($a);

?>

At this point, what is the name of the variable that holds "42"?? $b, $c,
both, or nobody??

For this issue, do not think that a variable is a variable. Think that a
variable is an entry in an ordered map (with internal pointers),
implemented as a hash table that can map two (or more) keys to the same
value.

Let me (try to) explain this concept in pseudo-C:

function foo()
{
int* a;
int* b;
inc* c;
a = b = c = malloc(sizeof(int));
*a = 42;
a = NULL;
}

This would have a similar behaviour than the PHP code I posted up there. In
PHP, variable names "point" to the value. A reference is a pointer to that
value. Storing a reference is making two entries on the variable hash
tables point to the same value. When no references to a value are found,
that memory is freed.

Variable management in PHP is quite complicated internally, with all that
duplicate-on-write stuff when making copies of complex variables. The kind
of stuff that operating systems do when you fork() a process.

I agree with Ewoud: you can transverse the $GLOBALS superglobal array to
search for your value, but if you have any references over there, you may
find lots of problems. Lots.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
5.1.2-1 generating this signature.
Uptime: 00:59:10 up 48 min, 1 user, load average: 0.78, 0.74, 0.74

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD+lyN3jcQ2mg3Pc8RAvKSAJ0WnvCHl3pnSbriFUUpt2 GaubcdYACfeTLZ
LN4aiZoWerYGDo+5lrF2Bk0=
=OrfI
-----END PGP SIGNATURE-----
Feb 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.