Trying to send a session variable to javascript

Ideally, I would like to send a session variable to javascript. I figured I would do something like this:


echo("<script type='text/javascript'>var id=".$_SESSION['id'].";alert(id);</script>";

On the page the php script generates I get the source exactly as expected


<script type='text/javascript'>var id=1234567890;alert(id);</script>

But it never alerts. If I try to access id in other ways, I get undefined. It’s like this if I put it in the head or the body. If I declare a variable without injecting a PHP variable into it, it works fine (e.g. if after I declar id I also put var a=1;alert(a); it will alert fine, or I can access the variable fine).

To my novice understanding of PHP, I thought it just generates a string and sends it to your computer for the browser to interpret. So how can a browser receive that string and know that some value was declared using a php variable and that isn’t allowed?