Skip to content
Related Articles

Related Articles

Improve Article

How to do HTML syntax highlighting inside PHP strings ?

  • Difficulty Level : Medium
  • Last Updated : 09 Jul, 2021
Geek Week

Syntax highlighting is the work of code editors such as Sublime Text, Visual Studio, Dev CPP, etc, which highlights all the different parts of the source code depending on their syntax by color, modified fonts, or through graphical changes. Since color highlighting these days is integrated into all common editors and development areas. Highlighting does not affect code performance but makes life easier for developers. Syntax highlighting improves the readability of the source for the developers.


In the above example, the code is written in Sublime Text Code editor, and it highlights the HTML tags in pink color and the string inside the tag is of white color. This is called syntax highlighting. 


PHP Strings: Strings can be seen as a stream of characters. For example, ‘G’ is a character and ‘GeeksforGeeks’ is a string. We have learned the basics of string data type in PHP Data types and Variables article.

There are four ways of creating strings in PHP: 

  1. Single-quote strings
  2. Double-quote strings
  3. Heredoc
  4. Nowdoc

HTML syntax highlighting inside a PHP string: HTML syntax in PHP works fine. We are going to discuss HTML syntax highlighting inside a PHP syntax. We can use Heredoc syntax to get HTML highlighted inside PHP.

Heredoc in PHP is a way to write large blocks of strings inside PHP, without the classic single quote, double quotes delimiters. It relies on <<< and an identifier that will also mark the end of the string.


$string = <<< IdentifierName
// Strings

Example 1: Syntax highlighting of HTML in PHP strings is not possible in single or double-quoted strings.

single and double quotes


echo "<b>Hello </b>";
echo "
    <h1>GeeksforGeeks </h1>
    <h2>Syntax highlighting in PHP strings with double quotes</h2>
echo '
    <h2>PHP </h2>
    Syntax highlighting  in PHP stringswith single quotes 


Using single and double quotes

Example 2: Syntax highlighting inside PHP String is possible using Heredoc.



echo "<b>This is PHP text</b>";
echo <<<HTML
    <!-- Insert your HTML code here -->
    <h1>GeeksforGeeks </h1>
    <h2>Syntax highlighting using heredoc PHP syntax</h2>

Note: The echo <<<HTML is a heredoc PHP syntax, and it highlights the code inside till HTML.



Output is the same as it is with single and double-quoted strings.

Example 3: The following code demonstrates Syntax highlighting inside PHP String is possible using Nowdoc.


echo "<b>This is PHP text</b>";
echo <<<'EOD'
    <!--Insert your HTML code here -->
    <h1>GeeksforGeeks </h1>
    <h2>Syntax highlighting using Nowdoc PHP syntax</h2>


Nowdoc syntax


Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :