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.
Example:
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 ‘neveropen’ 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
- Heredoc
- 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.
Syntax:
$string = <<< IdentifierName // Strings IdentifierName;
Example 1: Syntax highlighting of HTML in PHP strings is not possible in single or double-quoted strings.
PHP
<?php echo "<b>Hello </b>" ; echo " <h1>neveropen </h1> <h2>Syntax highlighting in PHP strings with double quotes</h2> "; echo ' <h2>PHP </h2> Syntax highlighting in PHP stringswith single quotes '; ?> |
Output:
Example 2: Syntax highlighting inside PHP String is possible using Heredoc.
PHP
<?php echo "<b>This is PHP text</b>" ; echo <<<HTML <!-- Insert your HTML code here --> <h1>neveropen </h1> <h2>Syntax highlighting using heredoc PHP syntax</h2> HTML; ?> |
Note: The echo <<<HTML is a heredoc PHP syntax, and it highlights the code inside till HTML.
Output:
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.
PHP
<?php echo "<b>This is PHP text</b>" ; echo <<< 'EOD' <!--Insert your HTML code here --> <h1>neveropen </h1> <h2>Syntax highlighting using Nowdoc PHP syntax</h2> EOD; ?> |
Output:
References: https://www.php.net/manual/en/language.types.string.php