title: PHP variable in javascript "Invalid or unexpected token"
body: <p>I'm having a problem trying to use a <code>PHP</code> <code>variable</code> within <code>JavaScript</code>. I keep getting the following message.</p>
  
  <p>"Invalid or unexpected token. Message:  Undefined variable: example."</p>
  
  <p>I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:</p>
  
  <pre><code>&lt;?php
      $example = '2';
  ?&gt;
  &lt;script type="text/javascript"&gt;
      var php_var = "&lt;?php echo json_encode($example); ?&gt;";
  &lt;/script&gt;
  </code></pre>
  
  <p>Does anyone have any suggestions? I have also tried the following <code>javascript</code> that results in the same problem:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var php_var = "&lt;?php echo $example; ?&gt;";
  &lt;/script&gt;
  </code></pre>