[PHP] Re: How to print variable name and contents
Bob and Ed and others, Thanks for all the responses. They really helped. -- Gil -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to print variable name and contents
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 (\nbrTRACE: $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 [EMAIL PROTECTED] Try this script, it works for me: $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 = $tbr /); // output my_trace('$my_var'); // note the use of single quotes here my_trace('$_ENV[COMPUTERNAME]'); // note where the single quotes are used here Hope it helps, Bob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to print variable name and contents
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 (\nbrTRACE: $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 [EMAIL PROTECTED] 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 = $tbr /); // 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: How to print variable name and contents
On Jun 17, 2005, at 9:18 AM, 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 (\nbrTRACE: $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 [EMAIL PROTECTED] Try this script, it works for me: $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 = $tbr /); // output my_trace('$my_var'); // note the use of single quotes here my_trace('$_ENV[COMPUTERNAME]'); // note where the single quotes are used here Hope it helps, Bob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Dunno if anyone's mentioned it yet, but have you had a look at debug_backtrace()? http://www.php.net/manual/en/function.debug-backtrace.php I use it a lot during development and you skip the eval() call. You should be able to write a simple function to pull the info out that you want, but I usually just do a print_r() on it and manually look through the output. Edward Vermillion [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to print variable name and contents
You can use $$var constuction, what display name of $var function my_trace($var){ echo $$var.'='.$var.'br'; } nntp.charter.net [EMAIL PROTECTED] / : news:[EMAIL PROTECTED] 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 (\nbrTRACE: $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 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php