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