The problem with just echoing a php sting for a javascript variable is escaping the sting properly for single and double quotes and line returns. I found this fix for my problem with an apostrophe in the string that closed out the quote early. This is a better option that trying to use addslashes. Use json_encode instead.
<script> var myvar = <?php echo json_encode($myVarValue); ?>; </script>