title: Defining PHP variable names using a loop and arrays - good practice?
body: <p>I need to define a set of variables to a series </p>
  
  <pre><code>$charta ='&lt;img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/aplha.png" alt ="" /&gt;';
  $chartb ='&lt;img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/bravo.png" alt ="" /&gt;';
  $chartc ='&lt;img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/charlie.png" alt ="" /&gt;';
  </code></pre>
  
  <p>etc ...</p>
  
  <p>Rather than define them all like this I want to use a loop to define them using an array</p>
  
  <pre><code>$names = array (
  'aplha' =&gt;  'a',
  'bravo' =&gt;   'b',
  'charlie' =&gt; 'c'
  );
  </code></pre>
  
  <p>So I tried this, after reading about variable variables in the PHP documentation</p>
  
  <pre><code>foreach($names as $k=&gt;$v){
  ${'chart' . $v} ='&lt;img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/'.$k.'.png" alt ="" /&gt;';
  }
  </code></pre>
  
  <p>And this works.</p>
  
  <p>My simple question is - is this good / acceptable practice? I explained my method to a more experienced programmer and they told me to find another way that didn't include variable variables, because they are bad practice - but I can't think of what's wrong with this, nor how to do it better.</p>
  
  <p>Thoughts?</p>