The Wayback Machine - https://web.archive.org/web/20240624214341/https://www.geeksforgeeks.org/how-to-do-html-syntax-highlighting-inside-php-strings/
Open In App

How to do HTML syntax highlighting inside PHP strings ?

Last Updated : 09 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 ‘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.

Syntax:

$string = <<< IdentifierName
// Strings
IdentifierName;

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

single and double quotes

PHP




<?php
      
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 
';
      
?>


Output:

Using single and double quotes

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

heredoc

PHP




<?php
      
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>
HTML; 
       
?>


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

Output:

heredoc

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>GeeksforGeeks </h1>
    <h2>Syntax highlighting using Nowdoc PHP syntax</h2>
EOD;      
  
?>


Output:

Nowdoc syntax

References: https://www.php.net/manual/en/language.types.string.php



Similar Reads

How to disable text selection highlighting using CSS?
Making text unselectable is an easy task. All we have to do is to disable the selectivity of the text on our webpage for all the browsers that the webpage is likely to be loaded on. There are some settings/commands used in CSS which are used for enabling/disabling functionalities on specific browsers. Like to disable a certain functionality on our
1 min read
Highlighting Dropdown Options in ReactJS
Highlighting options make the user experience better. Whenever the options are shown in the dropdown, and the user starts typing, the options should highlight for user convenience and it can be achieved using Material UI. Creating React Application And Installing Module: Step 1: Create a React application using the following command: npx create-rea
2 min read
Blaze UI Typography Highlighting Text
Blaze UI is a free open source UI Toolkit that provides a great structure for building websites quickly with a scalable and maintainable foundation. All components in Blaze UI are developed mobile-first and rely solely on native browser features, not a separate library or framework. It helps us to create a scalable and responsive website fast and e
2 min read
Explain the benefits of spread syntax &amp; how it is different from rest syntax in ES6 ?
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and objects into elements in the case of an array and key-value pairs in the case of an object. The spread syntax is represented by three dots (...) in JavaScript. Syntax: var my_var = [...array]; Benefits of using Spread syntax: 1. It allows us to include all elements
4 min read
Less.js Extend Syntax & Inside Ruleset
LESS.js is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables, and, so on, which help to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will see the basic usage of the extend feature in LESS.js, along with kno
2 min read
How to do syntax checking using PHP ?
Syntax checking is one of the most important tasks in programming. Our compiler checks our code and shows relevant errors if there is any in the code i.e. compile time, run time, syntax, etc. We can do the same thing i.e. syntax checking in PHP. In this article, we are going to learn how we can do syntax checking in PHP. The syntax is basically a s
2 min read
PHP | Basic Syntax
The structure which defines PHP computer language is called PHP syntax. The PHP script is executed on the server and the HTML result is sent to the browser. It can normally have HTML and PHP tags. PHP or Hypertext Preprocessor is a widely used open-source general-purpose scripting language and can be embedded with HTML. PHP files are saved with the
5 min read
ES6 Top features and syntax
ES6 or as it is officially called: ECMAScript2015 is a new JavaScript implementation and is arguably the hottest topic in the JS developer's conventions and meetups why it should not be: JavaScript rules the web and is gaining a foothold in every other field possible, be it robotics(nodebots), desktop applications(using ion framework), chatbots, et
3 min read
XML | Syntax
Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. C/C++ Code &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=
3 min read
jQuery Syntax
It is used for selecting elements in HTML and performing the action on those elements. Syntax: $(selector).action()$ sign: It grants access to jQuery. (selector): It is used to find HTML elements. jQuery action(): It is used to perform actions on the elements. Used to hide the current element.$(this).hide() Used to hide all &lt;p&gt; elements.$("p"
1 min read
three90RightbarBannerImg