-2

I want to make a little function for debugging purposes, that prints the contents of the variable in a human readable format.

The function is based on the pr() shortcut found in CakePHP framework, i.e. this one:

I have this so far:

function pr($var,$msg){
    $pr_debug=true;
    if($pr_debug){
        echo "<pre>";
        if($msg) echo "<b>".$msg.": </b>";
        if(gettype($var)=="array" || gettype($var)=="object" ) echo "<br>";
        print_r($var);
        echo "</pre>";
    }
}

which prints an optional message ($msg) before the variables content.

But what I want to add, is that if no message is sent, to also print the variable's NAME as such message, so i could get something like this:

$myvar="hello";
pr($myvar); 

//should output:

myvar: hello

So inside my function, how can I get the variable's name as a string so I can output it? i.e. with pr($foo); I need "foo", $name="biz"; pr($$name); I need "biz", and so on...

Preferably I want something that would work despite PHP globals configuration or any of that (which by the way I don't understand very well, so any help on GLOBALS stuff would be much appreciated).

Thanks!

Extra: here at stackoverflow, how do i format source code to get PHP formatting and colors? As of now, I simpy used the toolbar in the textarea and chose "code sample"...

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DiegoDD
  • 1,625
  • 4
  • 21
  • 32
  • 1
    You should accept a string as input instead of a variable, then use `$$var` to get the value of the variable and `$var` to get the name of the variable. – Asad Saeeduddin Apr 10 '13 at 20:20
  • 1
    Realize that when you have a function `function foo($a)`, `$a` may have originally been the return value from a function, an algebraic expression, a literal value, or any number of things which aren't named variables. – Sam Dufel Apr 10 '13 at 20:25
  • 1
    I've tried to outline some of the reasons why this isn't such a useful idea on the older question: http://stackoverflow.com/a/15936154/157957 – IMSoP Apr 10 '13 at 21:05

1 Answers1

1

What you want is variable variables... but in truth, you do NOT want them. They're almost as bad as "goto" in modern programing:

$foo = 'bar';
$baz = 'foo';
echo $$baz;   // bar
     ^^-- note the doubled $'s

Problem is, by the time you get to your debug code, the "source" of the varaible's name has been lost, e.g.

function debug($out) {
   global $$out;
   echo $out, ': ', $$out;
}

$foo = 'bar';
debug($foo);

this will not output "foo" as the name, because the name '$foo' was not passed in to the function. Only the VALUE of $foo, which is bar goes in.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • but if instead of calling debug($foo) i call it as debug("foo"), then it will work as expected, echoing "foo: bar", which is what i want! so, instead of passing a $var directly as the parameter, I only need to modify my function to receive a variable's NAME, as a string, and then use it with $$ to get it's content. Am I right? – DiegoDD Apr 10 '13 at 21:31
  • yes, but remember that within the function, that variable would be out of scope anyways. – Marc B Apr 10 '13 at 22:48