Home PHP String Functions htmlspecialchars() PHP htmlspecialchars() Function 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.
$safe = htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); Call What 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 htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string Parameters Parameter Description string Required. The input text to escape. flags Optional. Quote and substitution flags. Prefer ENT_QUOTES. encoding Optional. Character encoding. Prefer UTF-8.
Return Value Return Value Description string A string with special HTML characters escaped.
PHP Examples 1. Basic escape 2. Quotes + UTF-8 3. Round-trip Example 1 — Escape Tags and Ampersands Turn markup characters into entities. Symbols like © stay as-is (unlike htmlentities() ).
<?php
echo htmlspecialchars("<b>Hi</b> & ©", ENT_QUOTES, "UTF-8");
// <b>Hi</b> & © <b>Hi</b> & © How It Works Specials. <, >, and & become <, >, &.Symbols stay. © is left unchanged—only the XSS specials are encoded.Flags. ENT_QUOTES prepares quote encoding when quotes appear.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
echo htmlspecialchars("It's \"ok\"", ENT_QUOTES, "UTF-8");
// It's "ok" How It Works Single quote. ' becomes ' with ENT_QUOTES.Double quote. " becomes ".Why it matters. Stops quote breakout in attributes like title="…".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
$original = "<script>";
$escaped = htmlspecialchars($original, ENT_QUOTES, "UTF-8");
$decoded = htmlspecialchars_decode($escaped, ENT_QUOTES);
echo ($decoded === $original) ? "same" : "diff";
// same How It Works Escape. <script> becomes <script>.Decode. htmlspecialchars_decode() reverses those specials.Same flags. ENT_QUOTES on both sides keeps quotes consistent.Strict match. === confirms an exact round-trip.Frequently Asked Questions What does PHP htmlspecialchars() do? 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.
What is the syntax for htmlspecialchars()? 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').
What is the difference between htmlspecialchars() and htmlentities()? 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.
Why should I use ENT_QUOTES and UTF-8? 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.
When should I call htmlspecialchars()? 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.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion