title: Using PHP Variables in Javascript with JSON
body: <pre><code>&lt;?php
     $heros=array("Spiderman","Batman","Superman");
  ?&gt;
  
  &lt;script type="text/javascript"&gt;
     var heros = &lt;?php echo $heros;?&gt; // I don't want to do this.
     for(i=0; i&lt;3; i++)
     {  
        if(heros[i]=='Spiderman')
        {
          alert('Hey! I am Spiderman.');
        }
     }
  &lt;/script&gt;
  </code></pre>
  
  <p>I want to use a php array inside javascript for loop but i don't want to reopen php tags inside <code>&lt;script&gt;&lt;/script&gt;</code> tags. How can i use php variables in javascript?</p>