title: PHP with Javascript - What is the scope of PHP variables inside a <script></script> tag?
body: <p>I'm trying to mix php and javascript, because my javascript needs to access some php variables when the page loads.  I'm having trouble understanding something, and I've made a code example.</p>
  
  <pre><code>&lt;?php
  session_start();
  $test = 100;
  $_SESSION['test'] = 200;
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Test This Out&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt; Testing Javascript and PHP Mixed &lt;/h1&gt;
  &lt;p&gt;
  &lt;?php 
  echo("The value of \$test is $test and the value of \$_SESSION['test'] is ");
  ?&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;script type="text/javascript"&gt;
  &lt;?php 
  session_start();
  if(isset($test)) echo("document.write('Non-session variable exists and is $test &lt;br /&gt;');"); 
  else echo("document.write('Non-session variable does not exist&lt;br /&gt;');");
  if(isset($_SESSION['test'])) echo("document.write('Session variable exists &lt;br /&gt;');"); 
  else echo("document.write('Session variable does not exist&lt;br /&gt;');");
  ?&gt;
  &lt;/script&gt;
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Output looks like:</p>
  
  <pre><code>Testing Javascript and PHP Mixed
  
  The value of $test is 100 and the value of $_SESSION['test'] is
  
  Non-session variable exists and is 100
  Session variable exists 
  </code></pre>
  
  <p>So I'm trying to understand what types of php variables are available to the script.  It appears that the variable I've called $test is available, but even though it knows that </p>
  
  <pre><code>$_SESSION['test'] 
  </code></pre>
  
  <p>exists, if I try to output this value (in the exact same way I output the </p>
  
  <pre><code>$test
  </code></pre>
  
  <p>variable), the whole system hangs.</p>
  
  <p>My questions are:
   1. Can the php used in the javascript "see" variables I've defined earlier on in the page?
   2. Why is my attempting to print the </p>
  
  <pre><code>$_SESSION['test'] 
  </code></pre>
  
  <p>variable making the whole thing crash (nothing at all is rendered)?
   3. Is the second </p>
  
  <pre><code>session_start(), 
  </code></pre>
  
  <p>the one in the script, necessary?</p>
  
  <p>Thanks for any help.</p>