passing variables from php to javascript [duplicate]

just use php to print some dynamic javascript

<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>

There are multiple methods for providing the data to the client, such as:

  1. Echo the variables in your javascript, var userid = <?php echo $userid; ?>
  2. JSON'fy your variables and supply them to your javascript via AJAX/jQuery: $.getJSON(url, function(data){ var userid = data.userid; });

I typically utilize JSON as much as possible when trying to present server-side data to the client-side, as it helps to separate the different layers.