9

I'm using Sublime Text with the Pastels on Dark theme. My language of choice is PHP. How can I get HTML syntax highlighting inside PHP strings & heredoc syntax?

4
  • 3
    Still need to know how to do it with strings, but I found the answer to half of my question. In heredoc syntax you use the name of the language for the delimiters. I.E. $o = <<<HTML ....... HTML; Commented Aug 20, 2013 at 14:27
  • 1
    The above comment should be an answer, no? Commented Nov 23, 2013 at 19:12
  • If you're manipulating big HTML strings inside PHP, it feels like you're doing something weird and/or wrong. Any large chunks of HTML belong outside the <?php blocks, where it gets syntax-highlighted properly.. Commented Nov 24, 2013 at 7:09
  • When you add HTML outside of PHP then you have to turn output buffering on and the turn it off again to capture the HTML. Also there will be all these snippets like: <?php echo $something ?> It makes the code a lot less easy to read. I don't like your idea Mark. Commented Mar 21, 2014 at 17:31

3 Answers 3

25

Name your heredocs after the language you are using. This will syntax highlight in many text editors, including Sublime Text.

For example:

echo <<<HTML
<!-- put HTML here and it will have syntax highlighting -->
HTML;
Sign up to request clarification or add additional context in comments.

3 Comments

I also just realized you can use {$myAry['subIdx']} to put any kind of variable into your heredoc syntax. Function calls can't be placed inside them but you can set the return value to a variable before the heredoc, then use that.
Also works in VSCode. Amazing!
Thank you. I really wanted to use heredoc for PHP in VSCode due to the convenience of it but was turned off by the lack of syntax highlighting and code hints. You are the man!
-1

Just code outside of php so you can still see the HTML syntax color-coded, and then put that html inside the php once you are done.

1 Comment

Thanks for the suggestion, but "done" isn't something that really every happens to code at this company. There are always more modifications to be made.
-1

Wanted to add this as a comment to Ol' Reliable's answer but I am not allowed yet.

Whilst coding outside and then copying in can be a hassle, for people/editors without syntax highlighting in heredoc, an easy workaround is to temporarily add a closing php tag to the heredoc opening tag:

<?php
$myHtmlCode = <<<HTML?>
    <h1>I am Highlighted</h1>
    <p>Remove the closing php tag above to finish editing</p>
HTML;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.