Code sample showing how PHP’s heredoc syntax adds syntax highlighting to a Javascript snippet

Using Heredoc Syntax for Improved Syntax Highlighting

If you find yourself writing PHP code containing other languages (JS snippets, SQL queries, etc.), you can use PHP’s Heredoc syntax for improved syntax highlighting, at least in VS Code.

Basically, Heredoc syntax uses <<< to note the beginning of a string, followed by an identifier. Everything until the next instance of that identifier is treated as a string.

Here are examples:

You may not get autocompletion and other IDE features, but at least it’s easier to read.

What you choose for the identifier does impact the syntax highlighting…for example, if I had used SQL instead of JS in the example above, the code would have been highlighted differently. I suspect this is a feature of ProseMirror, though I haven’t dug into it to find out for sure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.