title: Pass a PHP string to a JavaScript variable (and escape newlines)
body: <p>What is the easiest way to encode a PHP string for output to a JavaScript variable?</p>
  
  <p>I have a PHP string which includes quotes and newlines.  I need the contents of this string to be put into a JavaScript variable.</p>
  
  <p>Normally, I would just construct my JavaScript in a PHP file, à la:</p>
  
  <pre><code>&lt;script&gt;
    var myvar = "&lt;?php echo $myVarValue;?&gt;";
  &lt;/script&gt;
  </code></pre>
  
  <p>However, this doesn't work when <code>$myVarValue</code> contains quotes or newlines.</p>