PHP htmlspecialchars() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, htmlspecialchars() converts &, <, >, and quotes into HTML entities for safe display.

The string is required. Flags and encoding are optional. Use this when printing untrusted text into HTML.

Syntax

Call htmlspecialchars on the string. Start with this line.

PHP
$safe = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
CallWhat it does
htmlspecialchars("<b>")Encodes angle brackets.
htmlspecialchars($text, ENT_QUOTES, 'UTF-8')Escapes quotes too for attributes.

Prefer ENT_QUOTES and UTF-8. Use htmlentities() only when you need a wider entity set.

Library declarations
PHP
htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string

Parameters

ParameterDescription
stringRequired. The input text to escape.
flagsOptional. Quote and substitution flags. Prefer ENT_QUOTES.
encodingOptional. Character encoding. Prefer UTF-8.

Return Value

Return ValueDescription
stringA string with special HTML characters escaped.

PHP Examples

Example 1 — Escape Tags and Ampersands

Turn markup characters into entities. Symbols like © stay as-is (unlike htmlentities()).

PHP
<?php
echo htmlspecialchars("<b>Hi</b> & ©", ENT_QUOTES, "UTF-8");
// &lt;b&gt;Hi&lt;/b&gt; &amp; ©

How It Works

  1. Specials. <, >, and & become &lt;, &gt;, &amp;.
  2. Symbols stay. © is left unchanged—only the XSS specials are encoded.
  3. Flags. ENT_QUOTES prepares quote encoding when quotes appear.
  4. UTF-8. Passing "UTF-8" keeps multi-byte characters correct.

Example 2 — Escape Quotes with ENT_QUOTES

Both single and double quotes become entities when ENT_QUOTES is set—safer inside HTML attributes.

PHP
<?php
echo htmlspecialchars("It's \"ok\"", ENT_QUOTES, "UTF-8");
// It&#039;s &quot;ok&quot;

How It Works

  1. Single quote. ' becomes &#039; with ENT_QUOTES.
  2. Double quote. " becomes &quot;.
  3. Why it matters. Stops quote breakout in attributes like title="…".
  4. Defaults. Pass flags explicitly—PHP 8.1+ changed default flag combinations.

Example 3 — Escape, Then Decode

Matching flags restore the original string after a round-trip.

PHP
<?php
$original = "<script>";
$escaped = htmlspecialchars($original, ENT_QUOTES, "UTF-8");
$decoded = htmlspecialchars_decode($escaped, ENT_QUOTES);

echo ($decoded === $original) ? "same" : "diff";
// same

How It Works

  1. Escape. <script> becomes &lt;script&gt;.
  2. Decode. htmlspecialchars_decode() reverses those specials.
  3. Same flags. ENT_QUOTES on both sides keeps quotes consistent.
  4. Strict match. === confirms an exact round-trip.

Frequently Asked Questions

htmlspecialchars() converts special HTML characters to entities so they display as plain text instead of markup. It encodes &, <, >, and quotes (depending on flags)—the standard defense against XSS when echoing data into HTML.
string htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true). The recommended pattern for web apps is htmlspecialchars($text, ENT_QUOTES, 'UTF-8').
htmlspecialchars() encodes only the core specials (&, <, >, quotes). htmlentities() encodes every character with an HTML entity equivalent—including ©, €, and accented letters. For typical output escaping, htmlspecialchars() is enough.
ENT_QUOTES encodes both single and double quotes, preventing attribute-breakout attacks. UTF-8 ensures multibyte characters are handled correctly. Always pass both explicitly instead of relying on PHP defaults.
Call it at output time—whenever you echo user-controlled or dynamic data into HTML. Do not use it as a substitute for prepared statements for SQL.

Next: implode()

Join array elements into a string with a separator.

implode tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful