htmlentities() vs htmlspecialchars() in PHP: Full Breakdown with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

htmlentities() vs. htmlspecialchars(): Battle of the Escaping Titans in PHP

\n\n

If you've ever dealt with outputting user data on a web page, you've likely heard of the functions htmlentities() and htmlspecialchars(). These are PHP's two main tools for protecting against XSS attacks and correctly displaying HTML characters. But what's the difference between them? Which one should a beginner choose, and which one should a pro choose? Let's figure it out once and for all.

\n\n

Imagine: you're writing a blog in PHP, and a user enters a comment: <script>alert('XSS')</script>. If you simply output this to the screen, the browser will execute the script. Disaster! This is where htmlspecialchars() and htmlentities() come to the rescue. But each one works differently.

\n\n

What do htmlspecialchars() and htmlentities() have in common?

\n\n

Both functions convert special HTML characters into their HTML entities. For example:

\n
    \n
  • & (ampersand) → &amp;
  • \n
  • < (less than) → &lt;
  • \n
  • > (greater than) → &gt;
  • \n
  • " (double quote) → &quot;
  • \n
  • ' (single quote) → &#039; (or &apos; depending on the flag)
  • \n
\n\n

Without them, any input with angle brackets or ampersands will break the layout or open the door for hackers.

\n\n

The Main Difference: Scope of Conversion

\n\n

Herein lies the secret. htmlspecialchars() is the economical fighter. It only converts the five main characters that have direct meaning in HTML:

\n
    \n
  • & → &amp;
  • \n
  • < → &lt;
  • \n
  • > → &gt;
  • \n
  • " → &quot;
  • \n
  • ' → &#039; (if the ENT_QUOTES flag is enabled)
  • \n
\n\n

While htmlentities() is a perfectionist. It converts all characters for which HTML entities exist. This includes not only angle brackets, but also currency symbols (€, £, ¥), mathematical signs (∑, ∂), letters with diacritical marks (é, ñ, ü), and even emojis (☺).

\n\n

Simple Example: The Difference in Practice

\n\n

Let's look at the code. Suppose we have a string with French text:

\n\n
<?php\n$text = "Café & thé <script>alert('test')</script>";\necho "htmlspecialchars: " . htmlspecialchars($text, ENT_QUOTES, 'UTF-8') . "<br>";\necho "htmlentities: " . htmlentities($text, ENT_QUOTES, 'UTF-8');\n?>
\n\n

Result:

\n
htmlspecialchars: Café & thé <script>alert('test')</script>\nhtmlentities: Café & thé <script>alert('test')</script>
\n\n

In this example, there is no difference because the characters "é" and other Latin letters have no special meaning in HTML. But let's try with characters that do have entities:

\n\n
<?php\n$text = "€ 100 <b>bold</b>";\necho "htmlspecialchars: " . htmlspecialchars($text, ENT_QUOTES, 'UTF-8') . "<br>";\necho "htmlentities: " . htmlentities($text, ENT_QUOTES, 'UTF-8');\n?>
\n\n

Result:

\n
htmlspecialchars: € 100 <b>bold</b>\nhtmlentities: &euro; 100 <b>bold</b>
\n\n

See? htmlentities() converted the euro symbol "€" into its entity &euro;. While htmlspecialchars() left it as is. Why? Because "€" is not a dangerous character for HTML — the browser will display it correctly even without escaping.

\n\n

When to use htmlspecialchars()?

\n\n

This is your default choice. Use htmlspecialchars() in 99% of cases when you output user

Blogs

Book Recommendations