title: Access PHP var from external javascript file
body: <p>I can access a PHP var with Javascript like this:</p>
  
  <pre><code>&lt;?php
      $fruit = "apple";
      $color = "red";
  ?&gt;
  
  &lt;script type="text/javascript"&gt;
      alert("fruit: " + "&lt;?php echo $fruit; ?&gt;"); // or shortcut "&lt;?= $fruit ?&gt;"
  &lt;/script&gt;
  </code></pre>
  
  <p>But what if I want to use an external JS file:</p>
  
  <pre><code>&lt;script type="text/javascript" src="externaljs.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>externaljs.js:</p>
  
  <pre><code>alert("color: " + "&lt;?php echo $color; ?&gt;");
  </code></pre>