You are here: RE: [PHP] Is there a way to get a variable name as a string? « PHP « IT news, forums, messages
RE: [PHP] Is there a way to get a variable name as a string?

Posted by "Daevid Vincent" on 07/19/05 05:03

That won't work. Then I will get what the VALUE of $bar is set to. I want
the actual text "bar". Basically everything after the "$" -- ie. The NAME of
the VARIABLE, not it's contents.

The closest I've come so far is to do this.


Function myname($foo)
{
echo "variable named $foo has the contents $$foo";
}

Myname('bar');

Which is pretty lame.


> -----Original Message-----
> From: Ryan A [mailto:ryan@coinpass.com]
> Sent: Monday, July 18, 2005 6:57 PM
> To: daevid@daevid.com
> Cc: php
> Subject: Re: [PHP] Is there a way to get a variable name as a string?
>
> Maybe something like:
>
>
> Function myname ($foo)
> {
> $return_value="the variable name passed in is ".$foo;
> return $return_value;
> }
>
> echo myname($bar);
>
>
> Just a guess.....
>
>
> On 7/19/2005 3:27:57 AM, Daevid Vincent (daevid@daevid.com) wrote:
> > Is there a way to get the name of a variable as a string?
> For example...
> >
> > Function myname ($foo)
> > {
> > echo "the variable name passed in is ".realname($foo);
> > }
> >
> > myname($bar);
> > ^^^^
> >
> > I want to see printed out:
> >
> > "the variable name passed in is bar"
> > ^^^
> >
> > Dig what
> > I'm trying to do?
> >
> > And 'why?' you may ask am I trying to do this... Well, I am
> sick of always
> > putting an "echo 'printing
> > out
> > bar:'; print_r($bar);" all the time. I want
> > to make a wrapper function.
> >
> > I also have written one for XML that would be nice to make
> a NAME='bar'
> > attribute...
> >
> > /**
> > * Print out an array in XML form (useful for debugging)
> > * @access public
> > * @author Daevid Vincent [daevid@daevid.com]
> > * @since 4.0b4
> > * @version 1.0
> > * @date 07/18/05
> > */
> > function print_r_xml($myArray)
> > {
> > print xmltag('ARRAY', array('NAME'=>'myArray'), 1);
> > foreach($myArray as $k => $v)
> > {
> > if (is_array($v))
> > print_r_xml($v);
> > else
> > print xmltag($k,htmlspecialchars($v));
> > }
> > print xmltag('ARRAY', null, 2);
> > }
> >
> > --
> > P
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация