Best Practices: Including PHP in your JavaScript code
<p>I'm a PHP phr34k addicted to JavaScript on the quest for some knowledge. If one were to include php code in their scripts, what would be the best method? I have provided some sample code as an example on how I would go about including PHP in my scripts. </p>

<p>Is this a valid method?</p>

<p><strong>index.php</strong></p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;PHP with JavaScript?&lt;/title&gt;
&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="test.js.php"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>test.js.php</strong></p>

<pre><code>&lt;?php 

$foo = 'bar';

?&gt;

$(document).ready(function() {
    var foo = '&lt;?php echo $foo; ?&gt;';
    alert(foo);
});
</code></pre>

<p>Thanks again!</p>
