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

Your name:

Reply:


Posted by "Daevid Vincent" on 07/22/05 22:56

> 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
* @param string $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
* @param string $xmltag is the tag name to surround
* @param mixed $data data to output between the tags, if $data is
an array, it will be expanded as KEY=VALUE in the tag
* @param int $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('&','&', $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";
}

[Back to original 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

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