How to do HTML syntax highlighting inside PHP strings ?
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:
- Single-quote strings
- Double-quote strings
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 IdentifierName;
Example 1: Syntax highlighting of HTML in PHP strings is not possible in single or double-quoted strings.
Example 2: Syntax highlighting inside PHP String is possible using Heredoc.
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.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.