The JavaScript script is just as much a part of the page as the HTML itself - PHP can write script in exactly the same way it writes markup.
If, for example, you want to set a Javascript variable from within PHP you can go
<?
print("var jsvariable = $phpvariable;");
?>
You can use loops to write entire Javascript arrays a la
<?
print('var jsarray = new Array(');
...loop through and print array elements...
print(');');
?>