How to Display HTML Tags as Plain Text using PHP?
Last Updated :
11 Jul, 2025
HTML tags begin with the less-than character and end with greater-than character, the text inside the tag formatted and presented according to the tag used. Every tag has special meaning to the browser but there are cases when to show plain HTML code in webpage. There are various methods in PHP to show the HTML tags as plain text, some of them are discussed below:
Method 1: Using htmlspecialchars() Function
The htmlspecialchars() function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities.
Syntax:
string htmlspecialchars( $string, $flags, $encoding, $double_encode )
- $string: This parameter is used to hold the input string.
- $flags: This parameter is used to hold the flags. It is combination of one or two flags, which tells how to handle quotes.
- $encoding: It is an optional argument which specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to PHP default version.
- $double_encode: If double_encode is turned off then PHP will not encode existing HTML entities. The default is to convert everything.
Return Values: This function returns the converted string. If there is invalid input string then empty string will returned.
Example:
php
<?php
echo("<b>without using htmlspecialchars() function</b><br>");
$myVar = htmlspecialchars(
"<b>using htmlspecialchars() function</b>", ENT_QUOTES);
echo($myVar);
?>
Output:

Method 2: Using htmlentities() Function
The htmlentities() function is an inbuilt function in PHP which is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entity.
Syntax:
string htmlentities( $string, $flags, $encoding, $double_encode )
Parameters: This function accepts four parameters as mentioned above and described below:
- $string: This parameter is used to hold the input string.
- $flags: This parameter is used to hold the flags. It is combination of one or two flags, which tells how to handle quotes.
- encoding: It is an optional argument which specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to PHP default version.
- $double_encode: If double_encode is turned off then PHP will not encode existing HTML entities. The default is to convert everything.
Return Values: This function returns the string which has been encoded.
Example:
php
<?php
$str = "<b>GeeksforGeeks</b>";
echo("without using htmlentities() function = ".$str."<br>");
$myVar = htmlentities($str, ENT_QUOTES);
echo("with using htmlentities() function = ".$myVar);
?>
Output:

Method 3: Using Character Entities
This method is used to replace the character by set of characters to get the desired output. In this method, < is replaced by < and > is replaced by >.
Example:
php
<?php
$str = "<b>GeeksforGeeks</b>";
echo("without using & lt; and & gt; = " . $str . "<br>");
$myVar = "<b>GeeksforGeeks</b>";
echo("with using & lt; and & gt; = " . $myVar);
?>
Output:
Similar Reads
How to Display HTML Tags as Plain Text in HTML? In HTML, certain characters like <, >, ", and & are reserved for defining HTML elements and attributes. To display these characters as plain text instead of interpreting them as part of the HTML structure, you need to use special codes called HTML entities.HTML Entities for Special Charact
2 min read
How to display string values within a table using PHP ? A table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. Tables are useful for various tasks such as presenting text information and numerical data.Tables can be used to compare two or more i
1 min read
How to define preformatted text using HTML? To define a preformatted text in HTML use the <pre> tag in the document. It is used to define the block of preformatted text that preserves the text spaces, line breaks, tabs, and other formatting characters that are ignored by web browsers. Text in the pre-tag is displayed in a fixed-width fo
1 min read
How to display long quotations in HTML ? In this article, we will discuss the tag that will display long quotations. The <blockquote> tag in HTML is used to display long quotations (a section that is quoted from another source). It changes the alignment to make it unique from others. It contains both opening and closing tags. In the
2 min read
How to write e-mails in HTML and send it using Gmail ? In this article, we are going to learn that how users can write e-mails in HTML format and send them using Gmail. However, Gmail doesn't offer an HTML editor still we can send HTML templates in an e-mail using some tools and methods. Many people need to send an e-mail with HTML templates to others.
3 min read
How to print on browser's console using PHP ? The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console. Syntax: echo "This is GeeksforGeeks Tutorial."; echo variable_name; Note: The echo command will print the HTML document. This feature can be used to put JavaSc
2 min read