Escaping Output (htmlspecialchars)

Ma Mahalakshmi V Updated 14 Nov 2026
7 min read ·Lesson 116 of 256

Why Output Must Be Escaped

In simple terms: When you print user-supplied text into a web page, the browser cannot tell your HTML apart from the user's text. If a user's "name" contains HTML or JavaScript, the browser will run it. Escaping converts special characters into safe codes, so the browser displays them instead of executing them.

Technically: Characters such as <, >, &, ", and ' have special meaning in HTML. htmlspecialchars() converts them into HTML entities (<, >, &, ", '). The browser renders entities as the original characters, but never interprets them as markup.

The Attack: Cross-Site Scripting (XSS)

<?php
$name = '<script>alert("XSS")</script>';   // submitted by an attacker
?>
<p>Unsafe: <?= $name ?></p>
<p>Safe: <?= htmlspecialchars($name) ?></p>

Output (HTML)

<p>Unsafe: <script>alert("XSS")</script></p>
<p>Safe: <script>alert("XSS")</script></p>

In the first line, the browser executes the script. In the second, the visitor simply sees the text <script>alert("XSS")</script>. A real attacker would not show an alert; the script could steal session cookies, change the page content, submit forms as the victim, or redirect to a phishing site. XSS is one of the most common web vulnerabilities, and consistent output escaping is the main defense.


htmlspecialchars()

Syntax

htmlspecialchars(
    string $string,
    int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
    ?string $encoding = null,       // defaults to UTF-8
    bool $double_encode = true
): string

Characters Converted

CharacterEntity
&&
<<
>>
""
''

Example

<?php
echo htmlspecialchars("O'Reilly & Sons <books>") . "\n";

Output

O'Reilly & Sons <books>

Default Flags (PHP 8.1+)

Since PHP 8.1, the default flags are ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401:

  • ENT_QUOTES escapes both double and single quotes. Before PHP 8.1, single quotes were not escaped by default, which made single-quoted attributes vulnerable. In code that must run on older versions, pass ENT_QUOTES explicitly.
  • ENT_SUBSTITUTE replaces invalid UTF-8 sequences with the replacement character � instead of returning an empty string.

Invalid UTF-8

<?php
var_dump(htmlspecialchars("abc\xFF"));
var_dump(htmlspecialchars("abc\xFF", ENT_QUOTES));

Output

string(6) "abc�"
string(0) ""

Without ENT_SUBSTITUTE, invalid input returns an empty string, so content silently disappears. The default flags in PHP 8.1+ avoid this. (The replacement character takes 3 bytes in UTF-8, so the result is 6 bytes long.)

Double Encoding

<?php
echo htmlspecialchars('Fish & Chips') . "\n";
echo htmlspecialchars('Fish & Chips', double_encode: false) . "\n";

Output

Fish &amp; Chips
Fish & Chips

By default, existing entities are escaped again, which displays the literal text & in the browser. Double encoding usually means the data was already escaped before it was stored — fix that at the source (store raw text, escape only on output) rather than using double_encode: false everywhere.


Escape for the Context

HTML is not the only context. Each place where data is inserted needs its own escaping:

ContextExampleEscaping
HTML text<p><?= ... ?></p>htmlspecialchars()
HTML attribute (quoted)<input value="<?= ... ?>">htmlspecialchars(), attribute must be quoted
URL query parameterhref="/search?q=<?= ... ?>"urlencode() (or http_build_query()), then htmlspecialchars()
Whole URL in href/src<a href="<?= ... ?>">Validate the scheme (http/https), then htmlspecialchars()
JavaScript data<script>const x = <?= ... ?>;</script>json_encode() with the JSON_HEX_* flags
CSSstyle="..."Avoid inserting user data into CSS
SQLQueriesPrepared statements (not escaping functions)

Attributes Must Be Quoted

<?php
$value = 'x onfocus=alert(1) autofocus';
?>
<input value=<?= htmlspecialchars($value) ?>>
<input value="<?= htmlspecialchars($value) ?>">

Output (HTML)

<input value=x onfocus=alert(1) autofocus>
<input value="x onfocus=alert(1) autofocus">

htmlspecialchars() does not escape spaces or =. In the unquoted attribute, the space ends the value, and the rest becomes new attributes — an onfocus handler that runs JavaScript. In the quoted attribute, the whole string stays inside the value. Always quote attributes.

URLs

<?php
$search = 'a&b <c>';
?>
<a href="/search?q=<?= htmlspecialchars(urlencode($search)) ?>">Search again</a>

Output (HTML)

<a href="/search?q=a%26b+%3Cc%3E">Search again</a>

urlencode() makes the value safe inside the URL (& would otherwise start a new parameter); htmlspecialchars() then makes the URL safe inside the HTML attribute. For URLs supplied entirely by users, also check the scheme — see PHP Form URL/E-mail.

Passing Data to JavaScript

<?php
$userName = '</script><script>alert(1)</script>';
$json = json_encode($userName, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
?>
<script>
    const userName = <?= $json ?>;
</script>

Output (HTML)

<script>
    const userName = "\u003C\/script\u003E\u003Cscript\u003Ealert(1)\u003C\/script\u003E";
</script>
  • json_encode() produces a valid JavaScript string literal, including the quotes.
  • JSON_HEX_TAG encodes < and > as \u003C and \u003E, so the value cannot close the <script> element early. The other JSON_HEX_* flags encode &, ', and ".
  • Do not use htmlspecialchars() inside <script>; HTML entities are not decoded there and would appear literally.

A safer alternative is to put data in a data- attribute (escaped with htmlspecialchars()) and read it in JavaScript with element.dataset.


Line Breaks: nl2br() After Escaping

<?php
$message = "Line 1\n<b>Line 2</b>";

echo nl2br(htmlspecialchars($message)) . "\n";

Output

Line 1<br />
<b>Line 2</b>

Escape first, then convert newlines to <br />. In the reverse order, htmlspecialchars() would escape the <br /> tags themselves.


A Short Helper: e()

Escaping must happen on every output, so a short helper reduces noise in templates:

<?php
function e(?string $value): string
{
    return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

Flags and encoding are passed explicitly, so the behavior is identical on every PHP version and configuration. Accepting null avoids deprecation notices when optional values are missing.


Practical Example: Displaying Comments Safely

<?php
function e(?string $value): string
{
    return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

$comments = [
    ['author' => 'Asha', 'text' => "Great lesson!\nThanks."],
    ['author' => '<img src=x onerror=alert(1)>', 'text' => 'Nice <b>tips</b>'],
];
?>
<?php foreach ($comments as $comment): ?>
<div class="comment">
    <strong><?= e($comment['author']) ?></strong>
    <p><?= nl2br(e($comment['text'])) ?></p>
</div>
<?php endforeach; ?>

Output (HTML)

<div class="comment">
    <strong>Asha</strong>
    <p>Great lesson!<br />
Thanks.</p>
</div>
<div class="comment">
    <strong><img src=x onerror=alert(1)></strong>
    <p>Nice <b>tips</b></p>
</div>

What Happens

  1. The first comment is displayed normally, with its line break converted to <br />.
  2. The second comment's "author" is an attack: an image tag whose onerror handler would run JavaScript. After escaping, it appears as harmless text.
  3. HTML formatting in comments (<b>) is shown literally rather than rendered. If your application should allow formatting, use a safe markup format (such as Markdown rendered by a library in safe mode) or an HTML sanitizer — never print raw HTML from users.

Escape on Output, Not on Input

ApproachProblem
Escape when saving to the databaseData becomes HTML-specific; it shows & in emails, CSV exports, APIs, and gets double-encoded in HTML
Escape when outputtingData stays raw and reusable; each output applies the correct escaping

Store the user's text exactly as validated, and escape it at the moment it is placed into HTML, a URL, JavaScript, or another context.

Template Engines Escape Automatically

Twig ({{ name }}) and Laravel Blade ({{ $name }}) escape output by default, which makes forgetting it much less likely. Their "raw" syntax ({{ name|raw }}, {!! $name !!}) disables escaping and should be used only for trusted HTML.


Common Mistakes

Forgetting to Escape "Safe-Looking" Data

Effect: XSS through names, titles, search terms, error messages, or data from APIs.

Fix: Escape every dynamic value, regardless of its source.

Unquoted Attributes

Effect: Attribute injection despite escaping.

Fix: Always use quotes around attribute values.

Escaping With the Wrong Function

Effect: htmlspecialchars() inside <script>, or addslashes() for HTML, leaves the context vulnerable.

Fix: Use the escaping method for the specific context.

Escaping Before Storing

Effect: Double-encoded text and broken non-HTML outputs.

Fix: Store raw data; escape on output.

Relying on strip_tags()

Effect: Remaining attributes and text can still be dangerous.

Fix: Escape with htmlspecialchars(), or use an HTML sanitizer for rich text.


Best Practices

  • Escape every dynamic value at output time with htmlspecialchars() (or an e() helper).
  • Quote all HTML attributes.
  • Use urlencode()/http_build_query() for URL parameters and json_encode() with JSON_HEX_* flags for JavaScript.
  • Validate URL schemes for user-supplied links.
  • Prefer template engines with automatic escaping, and use raw output only for trusted content.

Official reference: https://www.php.net/manual/en/function.htmlspecialchars.php

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Tutorial Index PHP Home PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope Static Variables Variable Variables PHP Echo / Print PHP printf / sprintf PHP Data Types PHP Booleans PHP NULL PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters String Interpolation Heredoc and Nowdoc Search in Strings Multibyte Strings PHP Floats PHP Casting PHP Type Juggling PHP Math PHP Random Numbers PHP Constants PHP Magic Constants PHP Operators Arithmetic Operators Assignment Operators Comparison Operators Increment / Decrement Operators Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence PHP If...Else...Elseif PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement Continue Statement PHP Functions User-defined Functions Function Arguments Default Argument Values Pass by Reference Named Arguments Variadic Functions Return Values Type Declarations in Functions Recursive Functions Variable Functions Anonymous Functions Closures Arrow Functions First-class Callable Syntax PHP Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Destructuring Array Spread Operator Search in Arrays Merge Arrays array_map / array_filter / array_reduce Array Functions PHP Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_COOKIE $_SESSION \$\_ENV PHP RegEx RegEx Patterns RegEx Modifiers preg_match preg_replace preg_split PHP RegEx Functions PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail Form Input Sanitizing Escaping Output (htmlspecialchars) Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting DateTime Class Timezones PHP Include Include vs Require PHP File Handling PHP File Open/Read PHP File Create/Write File Delete / Rename / Copy PHP Directories PHP CSV Files PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions Error Handling Custom Error Handler Error Logging HTTP Headers and Redirects Output Buffering Sending Email PHP cURL Consuming REST APIs PHP Generators PHP What is OOP PHP Classes/Objects The $this Keyword PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Destructor PHP Access Modifiers Readonly Properties PHP Inheritance Method Overriding The final Keyword PHP Class Constants PHP Abstract Classes PHP Interfaces Polymorphism PHP Traits PHP Static Methods PHP Static Properties Late Static Binding Magic Methods Object Cloning Comparing Objects Anonymous Classes PHP Enums PHP Namespaces Autoloading Classes PHP Iterables Iterators Object Serialization Strict Types Union Types Nullable Types Mixed and Never Types Nullsafe Operator PHP Attributes PHP Fibers Readonly Classes Property Hooks (PHP 8.4) Asymmetric Visibility (PHP 8.4) Reflection API SPL Data Structures MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data MySQL Joins with PHP Pagination with PHP PDO Introduction PDO Connect PDO Prepared Statements PDO Fetch Modes PDO Transactions PDO Error Handling PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser PHP XMLReader PHP XMLWriter AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll AJAX with JSON Security Introduction Password Hashing SQL Injection Prevention XSS Prevention CSRF Protection Session Security Secure Cookies File Upload Security Filesystem Security Error Reporting in Production Hiding PHP Encryption with Sodium PHP Configuration (php.ini) PHP Command Line (CLI) Composer PSR-4 Autoloading Installing Extensions (PECL / PIE) Debugging PHP OPcache & Performance Garbage Collection PHP-FPM Migrating to PHP 8 Project: Contact Form with Email Project: Login & Registration System Project: CRUD App with MySQL Project: Image Upload Gallery Project: Shopping Cart with Sessions Project: Simple Blog Project: REST API with PHP
Ask about this post
AI Ask about this post

Ask questions about Escaping Output (htmlspecialchars) and get answers drawn from it.

Signed-in readers only.