title: syntax error when outputting php to javascript
body: <p>This is working, after all it's a syntax error, but I was wondering how could one do this so that the syntax is proper. </p>
  
  <p>The code below is part of a script that is preparing variables to be sent on a Ajax call, so depending on the scope the corresponding variable should be passed on to Ajax.</p>
  
  <p>Problem, from javascript perspective, it seems, is that only the php variable for each scope is defined. </p>
  
  <p>I would think that it would not try at all to parse if the else if condition is not met, but apparently that is not the case. The php variable $scope is always defined.</p>
  
  <p>Both firebug and Chrome developer tool are throwing the error:</p>
  
  <pre><code>                    var scope_var = &lt;br /&gt;
  &lt;b&gt;Notice&lt;/b&gt;:  Undefined variable: brand in &lt;b&gt;/Applications/MAMP/htdocs/my_site/my_script.php&lt;/b&gt; on line &lt;b&gt;1049&lt;/b&gt;&lt;br /&gt;null;
  </code></pre>
  
  <p>Thanks</p>
  
  <pre><code>        var scope = &lt;?php echo json_encode($scope) ;?&gt;;
          if (scope == 'all') {var scope_var = 'all';}
          else if (scope == 'brands') {var scope_var = &lt;?php echo json_encode($brand) ;?&gt;;}
          else if (scope == 'cities') {var scope_var = &lt;?php echo json_encode($city) ;?&gt;;}
          else if (scope == 'models') {var scope_var = &lt;?php echo json_encode($model) ;?&gt;;}
          else if (scope == 'prange') {var scope_var = &lt;?php echo json_encode($used_prange_low) . '|' . json_encode($used_prange_high) ;?&gt;;}
  </code></pre>