Re: Print a variable's name - FIXED

From: Date: Wed, 05 May 2004 15:54:31 +0000
Subject: Re: Print a variable's name - FIXED
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-185446@lists.php.net to get a copy of this message
I didn't say SOLVED becuase this gets me "sor of" what I want : I added to the function an optional parameter so that if I reall wanted the var name to show I can pass it in... I know it's messy, but for debugging it's fine: function showvar($var,$varname="Var: ") { echo "Now Showing ".$varname.": <br>\n"; echo $var; } This way I supply the var name if I want to.. such as: $s1 = "Hello World"; showvar($s1,"s1"); or showvar($s1); thanks all Ahbaid. Dave Avent wrote:
$test = "Hello World!"; function showvar($var) { foreach($GLOBALS as $key => $value) { if($value == $var) { $varname = $key; } } echo "Variable Name: ".$varname."<br>\n"; echo "Variable Value: ".$var."<br>\n"; } showvar($test); This is the only thing that works for me.....I know it is messy -----Original Message----- From: Michael Sims [mailto:michaels@crye-leike.com] Sent: 05 May 2004 4:23 PM To: php-general@lists.php.net Subject: RE: [PHP] Print a variable's name Ahbaid Gaffoor wrote:
Thanks Ryan, but what I want is to be able to pass any variable to a procedure and have the variable name and value printed by the procedure. Can this be done? I'm trying to extend my library of debugging functions/procedures by having a procedure which can be used to "inspect" a variable whenever I call it.
This is a bit kludgy, but should work: function showvar($varname) { if (!isset($GLOBALS[$varname])) { return; } echo "Now showing: $varname\n"; echo "Value: ".$GLOBALS[$varname]."\n"; } $s1 = "Hello World"; showvar('s1'); HTH


« previous php.general (#185446) next »