title: Eval in replacing and resolving variables inside a PHP string
body: <pre><code>$html = '&lt;html&gt;&lt;body&gt;$DATA&lt;/body&gt;&lt;/html&gt;';
  
  $DATA = "&lt;h1&gt;Hi&lt;/h1&gt;";
  
  eval("\$html = \"$html\";");
  echo $html;
  </code></pre>
  
  <p>The above piece of code will resolve the variable of $DATA properly. While</p>
  
  <pre><code>$html = '&lt;html&gt;&lt;body&gt;$DATA&lt;/body&gt;&lt;/html&gt;';
  
  $DATA = "&lt;h1&gt;Hi&lt;/h1&gt;";
  
  $html = "$html";
  
  echo $html;
  </code></pre>
  
  <p>This piece of code will not. Why? What is the difference between these two?</p>
  
  <p>Isn't the <code>eval("\$html = \"$html\";");</code> equal to just <code>$html = "$html";</code> ?</p>
  
  <p>Why first one works while the other one doesn't?</p>
  
  <hr>
  
  <p>As in my above examples; <code>$DATA</code> is and must be defined after the <code>$html</code>. That's the case :). In other case I wouldn't even have a question and bother.</p>
  
  <p>But it's... why first one works while second one doesn't. And how to make the second one works? But primarly why it doesn't in fact work.</p>