Another option is that of PHP outputting a hidden element with the variable in it and then JS reading it.
For instance
<?php
echo '<input type="hidden" id="myvar" value='.$val.' />';
?>
and then in JS
var v = document.getElementById("myvar");
// do something with v.value
Of course this is easily spoofable by the client, so take 2 cautions:
1) use this only if it is not a problem for any user to be able to see the value of the variable (e.g. by looking at the source)
2) if the JS does anything that can be possibly "dangerous" e.g. does an asynchronous call to a PHP page that does something in the DB with that value, be sure to have proper checks in the second PHP page (NOT in the JS) to ensure that the value had not been tampered with