Most javascript files don't need php because they work client side and with the DOM but there are some particular cases in which it can be usefull. For instance, with some string translations, client info, selective script loading, etc...

When the script is only used in a particular page it can be written inside that page, there is no need to externalize it. If that script will be used in more than one place or it's inside a really big php file it may be a good idea to externalize it for better maintenance. So, how to do it? Lets see an example.

The container page

This is the page that will load the javascript file. The only different thing is that the javascript file will have the extension .php:

<!DOCTYPE html>  
<head lang="en">  
    <meta charset="UTF-8">
    <title>Javascript test</title>
<script type="application/javascript" src="script.js.php"></script>  

The javascript file with php

For this file we need to change the header response from a php type (or html) to a javascript type so the client browser will interpret it correctly. The rest of the file will be like all normal javascripts.

<?php header("Content-Type: text/javascript");?>

alert("Javascript file loaded correctly!");  

This is everything we need to change to get it working.

Sending variables

If we need to send some php variables to the script it can be achieved like any php GET parameter. Here it's a simple example:

<script type="application/javascript" src="script.js.php?name=marc"></script>  
    $name = $_GET['name'];
    //We can save the variable for the javascript or we can echo it directly when needed
    echo("var name = '".$name."'");
    header("Content-Type: text/javascript");

alert("Hello <?php echo $name ?>. Bye "+name);  

Have any doubts? Write a comment and i'll try to help.