You are here: Re: How to print variable name and contents « PHP « IT news, forums, messages
Re: How to print variable name and contents

Posted by Bob Winter on 06/17/05 18:51

Bob Winter wrote:
>
>
> nntp.charter.net wrote:
>
>> I want to write a trace procedure and call it with variable names, and
>> I am having trouble with the syntax. My goal is to have a procedure
>> that will echo lines such as:
>>
>> Trace: $myvar="the contents of $myvar
>>
>> My attempt that didn't work is to write a function:
>>
>> function my_trace($m){
>> echo ("\n<br>TRACE: $m = ");
>> eval("\$t=\"$m\";");
>> echo($t."<br>\n");
>> }
>>
>> and call it with statements like:
>>
>> my_trace("\$my_var");
>> my_trace("\$_ENV[\"COMPUTERNAME\"]");
>>
>> What am I doing wrong, and how should this be done? Also, should I
>> post to a different group?
>>
>> Thanks,
>> Gil Grodsky
>> ggrodsky@charter.net
>

Sorry, I was missing a closing '}' in my previous post -- it should have been:

////////////////////////////////

$my_var = 'Hello world!';

function my_trace($m){ // pay attention to the use of single vs double quotes throughout
$q = substr($m, 1); // chop off the leading '$' in the variable name
@eval("global \${$q};"); // need to use global to get value of local variables into this function
// also need @ to supress warning caused by brackets in superglobal variables
eval("\$t = \${$q};"); // assign value of orginal $m to $t
echo ("<br />TRACE: $m = $t<br />"); // output
}

my_trace('$my_var'); // note the use of single quotes here
my_trace('$_ENV["COMPUTERNAME"]'); // note where the single quotes are used here

/////////////////////////////

Bob

 

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

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