htmlentities() vs htmlspecialchars() Function in PHP - GeeksforGeeks

03 Mar, 2022
In this article, we will see what htmlentities() & htmlspecialchars() Function is used for & also understand their implementation through the examples.
htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities. 
Syntax:
string htmlentities( $string, $flags, $encoding, $double_encode )
Parameters value: 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 a combination of one or two flags, which tells how to handle quotes.
$encoding: It is an optional argument that specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to the 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: This example uses the htmlentities() function to transform all characters which are applicable to HTML entities.
PHP
<?php   // String convertible to htmlentities  $str = '<a href="https://www.geeksforgeeks.org">GeeksforGeeks</a>';   // It will convert htmlentities and print them  echo htmlentities( $str );?>
Output:
<a href="https://www.geeksforgeeks.org">GeeksforGeeks</a>
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 )
Parameter value:
$string: This parameter is used to hold the input string.
$flags: This parameter is used to hold the flags. It is a combination of one or two flags, which tells how to handle quotes.
$encoding: It is an optional argument that specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to the 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 an invalid input string then an empty string will be returned. 
Example: This example uses the htmlspecialchars() function to convert all predefined characters to HTML entities. 
PHP
<?php   // String to be converted  $str = '"geeksforgeeks.org" Go to GeeksforGeeks';   // Converts double and single quotes  echo htmlspecialchars($str, ENT_QUOTES);?>
Output:
"geeksforgeeks.org" Go to GeeksforGeeks
Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.
bhaskargeeksforgeeks
rkbhola5
PHP-function
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Comparing two dates in PHP
PHP | Converting string to Date and DateTime
Download file from URL using PHP
Express.js express.Router() Function
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to set the default value for an HTML <select> element ?
Top 10 Angular Libraries For Web Developers