title: Most efficient way to pass PHP variables to external JavaScript (or jQuery) file
body: <p>I've read several posts about the question and found <a href="https://stackoverflow.com/a/10889334">this</a> to be the simplest solution, here is my code:</p>
  
  <p>js <strong>inside PHP</strong> code</p>
  
  <pre><code>&lt;script&gt;
      &lt;!--// 
        var jsBaseurl = &lt;?php echo json_encode(BASE_URL."/"); ?&gt;;
        var jsTitle = &lt;?php echo json_encode($h1_title); ?&gt;;
        var jsSubtl = &lt;?php echo json_encode($h2_title);?&gt;;
      //--&gt;
  &lt;/script&gt;
  &lt;script src="external.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>and the <strong>external</strong>.js</p>
  
  <pre><code>var siteURL=jsBaseurl;
  alert(siteURL+jsTitle+jsSubtl)
  </code></pre>
  
  <p>it works fine, <strong>my question is regarding to the following comments</strong> by <a href="https://stackoverflow.com/users/1402846/pang">Pang</a> and <a href="https://stackoverflow.com/users/1287020/biplav">biplav</a>:</p>
  
  <blockquote>
    <p>WARNING: This can kill your website. Example: </p>
  
  <pre><code>&lt;?php $myVarValue = '&lt;!--&lt;script&gt;'; ?&gt;
  </code></pre>
    
    <p>See <a href="https://stackoverflow.com/q/20942452/1402846">this question</a> for details. Solution: Use
        <a href="http://www.php.net/manual/en/json.constants.php#constant.json-hex-tag" rel="nofollow noreferrer">JSON_HEX_TAG</a> to escape &lt; and > (requires PHP 5.3.0). - <a href="https://stackoverflow.com/users/1402846/pang">Pang</a></p>
    
    <p>Another downside of this is that impacts the inital page load time. -
    <a href="https://stackoverflow.com/users/1287020/biplav">biplav</a></p>
  </blockquote>
  
  <p>I would like to know a simple solution for Pang's comment and how this all impacts the performance (page load time). Thanks a lot!</p>