Difference between htmlspecialchars and htmlentities

What is the difference between htmlspecialchars and htmlentities PHP functions?



This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.


Some characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made.If you require all HTML character entities to be translated, use htmlentities() instead.


1. htmlspecialchars may be used when there is no need to encode all characters which have their HTML equivalents.


2. htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.


In Short, you can say


htmlspecialchars(): Convert some special characters to HTML entities (Only the most widely used)

htmlentities(): Convert ALL special characters to HTML entities



One thought on “Difference between htmlspecialchars and htmlentities”

Comments are closed.