ClarkySmiverz77 ClarkySmiverz77 - 3 months ago 7
PHP Question

Prevent breaking Syntax when using single quote in php

I have a php file that contains html. In the php file I echo sections of the html(Not the best way I know, but good for the time being).

I have started to apply AngularJs to this project and I am having a few problems with the syntax breaking in php.

for example ...

echo="
<!-- AngularJS -->
<div class='container' style='border: 1px solid black'>
<div class='row' ng-app='productsApp' ng-controller='ProductsController'>

<div ng-repeat='product in products | orderBy : product.id | limitTo : 3' class='col-md-4' style='border: 1px solid; padding: 20px;'>
<h4>{{ product.name }}<h4>
</div>

</div>
</div>
";


So referring to the example above, i'd like to alter the html to orderBy : '-product.id' ... but the single quotes is causing syntax errors.

Can anyone advise how to get around this problem?

Thanks in advance.

Answer

When outputting blocks of text, prefer using HEREDOC/NOWDOC:

echo <<<'END'
    <!-- AngularJS -->
    Rest of HTML here
END;

This works well because you don't have to worry about nested quotes.

Comments