The following solution should work quite well for what you are trying to do.
The JavaScript block is placed very late in the document so you don't have to worry about elements not existing.
You are setting a PHP variable at the top of the script and outputting just the value of the variable within the JavaScript block.
This way, you don't have to worry about escaping double-quotes or HEREDOCS (which is the recommended method if you REALLY must go there).
Javascript Embedding Example
<div id="helloContainer"><div>
<script type="text/javascript">
document.getElementById('helloContainer').innerHTML = '<?= $greeting; ?>';
</script>