Re: [PHP] Is there a way to get a variable name as a string?

2005-07-22 Thread Richard Lynch
On Mon, July 18, 2005 8:24 pm, Ryan A said:
 I didnt totally understand you q in the beginning (and still dont fully),
 but

?php
  $x = 5;
?

He wants a function that, if you put in $x, you get out 'x'

For *ANY* $variable.

There is no such function.

Usually the person asking it is doing something very newbie-ish, and very
wrong.

I know, cuz I was that person, many many years ago. :-)

I think what I needed that time was variable variables...

The other thing that tends to bring up this question is somebody trying to
write a custom error handler, not knowing about
http://php.net/trigger_error and friends.  Though that only gets you the
line number and filename where the error occured, not a specific variable
name.

If for some reason one *NEEDS* to have names and variables and to know
what a variable's name is, you *probably* want to use an associative
array and use the keys as your names.

Hopefully this is helpful to the Original Poster, or somebody some day.

PS

It's true that your variable could/would/should appear in debug_backtrace,
but how would you pick it out from all the other variables that would
appear in your debug_backtrace?

For that matter, it's in $_GLOBALS, but how would you pick it out?

You could print out all variables that were equal to your variable, and
have a list of candidates...

?php
  $x = 5;
  $y = 3;
  $z = 5;
  while (list($k, $v) = each($GLOBALS)){
if ($v === $x) echo $k might be your variable...br /\n;
  }
?

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-22 Thread Edward Vermillion

Richard Lynch wrote:

[snip]



PS

It's true that your variable could/would/should appear in debug_backtrace,
but how would you pick it out from all the other variables that would
appear in your debug_backtrace?

For that matter, it's in $_GLOBALS, but how would you pick it out?

You could print out all variables that were equal to your variable, and
have a list of candidates...

?php
  $x = 5;
  $y = 3;
  $z = 5;
  while (list($k, $v) = each($GLOBALS)){
if ($v === $x) echo $k might be your variable...br /\n;
  }
?



What I was thinking with debug_backtrace() is that you could get the 
information for the function that called the function you want the 
variable name for, *reducing* the likelyhood of duplicate values, but 
admitedly not eliminating it.


You could also pass the name of the variable to the function:

someFunction($foo, $varName=){ print $varName; }

call it with - someFunction($bar, 'bar');

That would definately get you what you want, but again it would be messy 
to look at.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Is there a way to get a variable name as a string?

2005-07-22 Thread Daevid Vincent
 What I was thinking with debug_backtrace() is that you could get the 
 information for the function that called the function you want the 
 variable name for, *reducing* the likelyhood of duplicate values, but 
 admitedly not eliminating it.
 
 You could also pass the name of the variable to the function:
 
 someFunction($foo, $varName=){ print $varName; }
 
 call it with - someFunction($bar, 'bar');
 
 That would definately get you what you want, but again it 
 would be messy to look at.

Well if you called someFunction('bar');

Function someFunction($myVar)
{
global $$myVar;
echo the variable name is '$myVar' and has the value of .$$myVar;
}

But that relies on the global and is also a bit messy in a recursive
situation.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Is there a way to get a variable name as a string?

2005-07-22 Thread Daevid Vincent
 He wants a function that, if you put in $x, you get out 'x'
 
 For *ANY* $variable.
 
 There is no such function.


 Usually the person asking it is doing something very 
 newbie-ish, and very wrong.

Actually it's not either...

Since you can't easily debug when generating XML, as malformed XML will STB,
I have this function which dumps out an array and recursively sub arrays.

It would be very useful to dump out the name of the variable as part of
this, as a lot of text is on the screen with all the tags and such. 

It always annoyed me about print_r() that it doesn't tell you the variable
name either, so you have to always prefix it with an echo/print just above
the print_r.

/**
* Print out an array in XML form (useful for debugging)
* @access   public
* @paramstring $myArray the array to output in XML format
* @version  1.0
* @date 07/19/05
* @todo It would be nice if we could extract the array's variable
name and output that as an attribute
*/
function print_r_xml($myArray)
{
print xmltag('ARRAY', null, 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);
}

/**
* Add the starting, or ending, or both xml tags
*
* @return   string xml formatted tags and data
* @paramstring $xmltag  is the tag name to surround
* @parammixed $data data to output between the tags, if $data is
an array, it will be expanded as KEY=VALUE in the tag
* @paramint $tagType 1=start tag only, 2=end tag only, 3=default
* @version  1.2
* @date 06/14/05
*/
function xmltag($xmltag, $data, $tagType = 3) 
{
// remove spaces and make uppercase
$xmltag = str_replace(' ','_', $xmltag );
//$xmltag = strtoupper( $xmltag );

$data = str_replace('','amp;', $data );   // fix data with 
$data = str_replace('','lt;', $data );// fix data with 
$data = str_replace('','gt;', $data );// fix data with 

if ($tagType == 3) 
{
if (isset($data))
{
if (is_array($data))
{
$tmp = ''.$xmltag;

if (is_array($data))
{
foreach($data as $key = $value)
$tmp .= '
'.$key.'='.$value.'';
}

$tmp .=  /\r\n;

return $tmp;
}
else
return
''.$xmltag.''.$data.'/'.$xmltag.\r\n;
}
else
return ''.$xmltag. /\r\n;
}

if ($tagType == 1)  
{
$tmp = ''.$xmltag;

if (is_array($data))
{
foreach($data as $key = $value)
$tmp .= ' '.$key.'='.$value.'';
}

$tmp .= \r\n;

return $tmp;
}

if ($tagType == 2)  return '/'.$xmltag.\r\n;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-19 Thread Burhan Khalid

Rasmus Lerdorf wrote:

Daevid Vincent wrote:


Is there a way to get the name of a variable as a string? For example...



Nope, not possible.


Well

ob_start();
echo '$var';
$contents = ob_get_contents();
ob_end_clean();

echo 'Variable Name is : '.substr($contents,strpos($contents,'$')+1);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Ryan A
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 ([EMAIL PROTECTED]) 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 [EMAIL PROTECTED]
 * @since 4.0b4
 * @version 1.0
 * @date07/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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Daevid Vincent
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:[EMAIL PROTECTED] 
 Sent: Monday, July 18, 2005 6:57 PM
 To: [EMAIL PROTECTED]
 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 ([EMAIL PROTECTED]) 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 [EMAIL PROTECTED]
  * @since 4.0b4
  * @version 1.0
  * @date07/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
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Rob Agar
hi Daevid

FWIW, I was trying to do the exact same thing a while back, and came to
the conclusion that it wasn't possible.

Rob

 -Original Message-
 From: Daevid Vincent [mailto:[EMAIL PROTECTED] 
 Sent: Tuesday, 19 July 2005 11:28 AM
 To: php-general@lists.php.net
 Subject: [PHP] Is there a way to get a variable name as a string?
 
 
 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 [EMAIL PROTECTED]
 * @since  4.0b4
 * @version1.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);
 }
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Rasmus Lerdorf
Daevid Vincent wrote:
 Is there a way to get the name of a variable as a string? For example...

Nope, not possible.

-Rasmus

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Ryan A
I didnt totally understand you q in the beginning (and still dont fully),
but

  Is there a way to get the name of a variable as a string? For example...

 Nope, not possible.
 -Rasmus

the man has spoken :-D

-Ryan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Edward Vermillion

Rasmus Lerdorf wrote:

Daevid Vincent wrote:


Is there a way to get the name of a variable as a string? For example...



Nope, not possible.

-Rasmus

Wouldn't the name of the variable show up in a var_dump()? It would be 
messy, but if it's there...


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Edward Vermillion

Edward Vermillion wrote:

Rasmus Lerdorf wrote:


Daevid Vincent wrote:


Is there a way to get the name of a variable as a string? For example...




Nope, not possible.

-Rasmus

Wouldn't the name of the variable show up in a var_dump()? It would be 
messy, but if it's there...




Actually I meant debug_backtrace()... Damn fingers... but it is still a 
question. :)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Is there a way to get a variable name as a string?

2005-07-18 Thread Tyler Kiley
function named_print($var_name) {
  return echo 'the variable named $var_name is set to ' . \$var_name;
}

eval(named_print($foo));

;-)

Tyler

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php