Q:

Extend chart.js in PHP heredoc causes parse error

I create a chart with chart.js in PHP which looks like this:
$chart = <<<ECHO
<script type="text/javascript">
var ctx = document.getElementById("myChart").getContext("2d");

Chart.types.Bar.extend({
    name: "myChart",
    initialize: function () {
        Chart.types.Bar.prototype.initialize.apply(this, arguments);
    }
});

new Chart(ctx).myChart(data, {
    scaleLabel : "<%= value + ' $' %>"
});

</script>
ECHO;
echo $chart;

This code works fine if I run it in Javascript but unfortunately I need to display it via PHP (I use a component in a CMS) which doesnt work. I get the following error:
Parse error: syntax error, unexpected '=', expecting '}' in process.php(1227) : eval()'d code on line 154
I narrowed the error down to the initialize: function() {...} line of code. But why does it cause problems within the PHP echo?

A:

I'm not sure why you're having problems, I don't think you should be.
But I have a better answer for you, use buffers instead of heredocs (they're easier to work with anyway)
Google ob_start to learn how it works, they're super useful
ob_start()?>
<script type="text/javascript">
var ctx = document.getElementById("myChart").getContext("2d");

Chart.types.Bar.extend({
    name: "myChart",
    initialize: function () {
        Chart.types.Bar.prototype.initialize.apply(this, arguments);
    }
});

new Chart(ctx).myChart(data, {
    scaleLabel : "<%= value + ' $' %>"
});

</script>
<?php $chart = ob_get_clean();
echo $chart;

