title: Using Javascript to access PHP variables
body: <p>How can I access <code>PHP</code> variables from <code>Javascript</code> where the <code>PHP</code> code exists in a separate file?  </p>
  
  <p>The code I have is:  </p>
  
  <pre><code>&lt;?php
  $res = "OK";
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
     &lt;script type="text/javascript"&gt;
     function myFunc() 
     {
        res = &lt;?php echo json_encode($res); ?&gt;;
        console.log("res = " + res);
     }
     &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;button onclick="myFunc()"&gt;Click&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Now, if I were to move my <code>php</code> code to a separate file called <code>a.php</code>, how can I access <code>$res</code>? Assume I am calling <code>a.php</code> with a <code>GET</code> request (<code>XMLHttpRequest</code> object).</p>