title: Get the name of a variable passed into a PHP function?
body: <p>I just threw this together to help in debugging some PHP scripts.  As you can see, it is sloppy but I am going to improve it some more.  </p>
  
  <p>My debug function has 2 variables passed in, a variable name and a variable value.  </p>
  
  <p><strong>Is it possible to just pass in the variable and somehow get the name of the variable without manually doing it like I have it set now?</strong></p>
  
  <p><strong>The Function</strong></p>
  
  <pre><code>&lt;?php
  function debug($varname, $var)
  {
      echo '&lt;br&gt;' . $varname;
  
      // $var is a STRING
      if (is_string($var)) {
          echo ' (string) = ' . $var . '&lt;br&gt;';
  
      // $var is an ARRAY
      } elseif (is_array($var)) {
          echo ' (array) = &lt;pre&gt;';
          print_r($var);
          echo '&lt;/pre&gt;&lt;br&gt;';
  
      // $var is an INT
      } elseif (is_int($var)) {
          echo ' (int) = ' . $var . '&lt;br&gt;';
  
      // $var is an OBJECT
      } elseif (is_object($var)) {
          echo ' (object) = &lt;pre&gt;';
          var_dump($var);
          echo '&lt;/pre&gt;&lt;br&gt;';
      }
  }
  </code></pre>
  
  <p><strong>The Test</strong></p>
  
  <pre><code>$testString = 'just a test!';
  $testArray = array(
      'key1' =&gt; 'value1',
      'key2' =&gt; 'value2',
      'key3' =&gt; 'value3'
      );
  $testInt = 1234567890;
  
  $testObject = new stdClass;
  $testObject-&gt;someVar1 = 'testing123';
  $testObject-&gt;someVar2 = '321gnitset';
  
  debug('$testString', $testString);
  debug('$testArray', $testArray);
  debug('$testInt', $testInt);
  debug('$testObject', $testObject);
  ?&gt;
  </code></pre>
  
  <p><strong>The Result...</strong></p>
  
  <pre><code>$testString (string) = just a test!
  
  $testArray (array) = 
  Array
  (
      [key1] =&gt; value1
      [key2] =&gt; value2
      [key3] =&gt; value3
  )
  
  
  
  $testInt (int) = 1234567890
  
  $testObject (object) = 
  object(stdClass)#1 (2) {
    ["someVar1"]=&gt;
    string(10) "testing123"
    ["someVar2"]=&gt;
    string(10) "321gnitset"
  }
  </code></pre>