My environment uses templates so this is not copy and paste code. However I was able to pass the variable to Javascript by doing this:
$teststring = 'mystring';
$page_headers = <<<PAGEHEADERS
<script>
window.onload=function(){
var testvar = '$teststring';
alert(testvar);
};
</script>
PAGEHEADERS;
As long as the php variable is defined first you should be able to get a value from it simply by calling it.