如何将变量从PHP传递到外部JS文件?

I'm trying to pass 2 variables (which contain data for a chart) to an external JS file (which contains settings for the chart).

In PHP I have:

<script type='text/javascript'>
var  final_values = '$final_values';
var  final_names = '$final_names';
</script>

and a part of external JS:

var rawData = "["+final_values+"]";
var ticks = "["+final_names+"]";
var plot = $.plot('#placeholder', [
    { data: rawData ,color: 'rgb(50,179,234)'}
  ], {
    series: {
      bars: {
        show: true, align: 'center', barWidth:0.7
      }
    },
    bars: {
      align: 'center',
      barWidth: 0.3,
      horizontal: true,
      fillColor: { colors: [{ opacity: 0.4 }, { opacity: 1}] },
      lineWidth: 1
    },
    grid: {
      backgroundColor: 'transparent',
      hoverable: true,
      clickable: true
    },
    xaxis: {
      autoscaleMargin: 0.1,
      tickColor: '#5E5E5E',                       
      color:'black',
      ticks: 5
    },
    yaxis: {
      position: 'left',
      ticks: ticks,
      tickSize: 1,
      color:'black'
    }
  });

Examples that don't work:

final_values = '[209,0],[570,1],[1359,2],[2692,3]';
final_names = '[0,"TheDude"],[1,"nikodemus"],[2,"adrianp55"],[3,"Richard Nasta"]';

If I put all JS settings inside the page, it works, but I want to put all the settings inside an external file.

json_encode.

var  final_values = <?php json_encode($final_values);?>;
var  final_names = <?php json_encode($final_names);?>;

warning though, unlike most of PHP, json_encode is not binary safe (beats me why, its not a limitation of JSON afaik), thus make sure you only have utf8-safe characters in whatever you're encoding, eg, this won't work:

json_encode(["foo"=>1,"bar"=>"\x00\xFF"] );

-- just because ["bar"] here is not valid utf8, the whole encoding failed :| stupid php.