title: PHP - how to get a variable's name?
body: <p>I want to make a little function for debugging purposes, that prints the contents of the variable in a human readable format.</p>
  
  <p>The function is based on the <code>pr()</code> shortcut found in CakePHP framework, i.e. <a href="http://api.cakephp.org/2.0/source-function-pr.html#214-228" rel="nofollow">this one</a>:</p>
  
  <p>I have this so far:</p>
  
  <pre><code>function pr($var,$msg){
      $pr_debug=true;
      if($pr_debug){
          echo "&lt;pre&gt;";
          if($msg) echo "&lt;b&gt;".$msg.": &lt;/b&gt;";
          if(gettype($var)=="array" || gettype($var)=="object" ) echo "&lt;br&gt;";
          print_r($var);
          echo "&lt;/pre&gt;";
      }
  }
  </code></pre>
  
  <p>which prints an optional message  ($msg) before the variables content.</p>
  
  <p>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:</p>
  
  <pre><code>$myvar="hello";
  pr($myvar); 
  </code></pre>
  
  <p>//should output:</p>
  
  <pre><code>myvar: hello
  </code></pre>
  
  <p>So inside my function, how can I get the variable's name as a string so I can output it?
  i.e. with <code>pr($foo);</code> I need <code>"foo"</code>, 
  <code>$name="biz"; pr($$name);</code> I need <code>"biz",</code>
  and so on...</p>
  
  <p>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).</p>
  
  <p>Thanks!</p>
  
  <p>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"...</p>