title: Getting PHP variable name as string for many variables
body: <p>I have searched through many many topics and none have helped me with my specific need as yet. </p>
  
  <p>I tried many options from <a href="https://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php">This Thread</a> but none have been quite right.</p>
  
  <p>I am making a config generator for switches and routers, 
  I am trying to streamline it, so rather than have a config array for each interface, i'll have config array for each type of layout and have the interface name as a variable.
  I have the web page posting to the script and I have the page posting the port layout per post, with each interface as its own variable. </p>
  
  <p>i.e.</p>
  
  <pre><code>$fa1 = "DESKTOP";
  $fa2 = "VOIP";
  $fa3 = "DESKTOP";
  $fa4 = "";
  $fa5 = "DESKTOP";
  </code></pre>
  
  <p>etc. I am trying to use a foreach loop to go through each interface (as such)</p>
  
  <pre><code>$ints = array($fa1,$fa2,$fa3,$fa4,etc);
  foreach ($ints as $line) {
      if ($line == "PC") {
          file_put_contents($file, $pcarray.FILE_APPEND);
      }
      if ($line == "VOIP") {
          file_put_contents($file,$voiparray.FILE_APPEND);
      }
  }
  </code></pre>
  
  <p>with the interface setup as an array:</p>
  
  <pre><code>$pcarray = array(
  'interface ' .$interface,
  'configetc'
  );
  </code></pre>
  
  <p>I need a function to get the name of the variable for the interface so I can assign it to the $interface variable. </p>
  
  <p>all the examples I've found and tried thus far dont seem compatible with like valued variables.</p>
  
  <p>ie. for the above, most would return fa1 for everything that comes up DESKTOP rather than fa3 for the next desktop one.</p>
  
  <p>an example of the ones that dont quite work how I want is:</p>
  
  <pre><code>function print_var_name($var) {
      foreach($GLOBALS as $var_name =&gt; $value) {
          if ($value === $var) {
              return $var_name;
          } 
      } 
      return false;
  }
  </code></pre>
  
  <p>This one, for each variable value repetition after the first just returned the first variables name.</p>
  
  <p>EDIT:
  What I am wanting to get back would be:</p>
  
  <pre><code>(as the written array)
  interface fa1
  standard desktop config
  interface fa2
  standard voip config
  interface fa3
  standard desktop config
  interface fa5
  standard desktop config
  </code></pre>
  
  <p>where as currently im getting:</p>
  
  <pre><code>interface fa1
  standard desktop config
  interface fa2
  standard voip config
  interface fa1
  standard desktop config
  interface fa1 
  standard desktop config
  </code></pre>