Content · Programming · PHP

PHP htmlspecialchars() Function: Usage and Examples

Learn htmlspecialchars() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to escape the HTML-special characters in a string. This comes up when preparing data, displaying results, or integrating information with other services.

The htmlspecialchars() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is htmlspecialchars() in PHP?

Escape the HTML-special characters in a string.

Its syntax is:

htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, ?string $encoding = null, bool $double_encode = true): string

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: Escape at output time for the correct HTML context. Do not confuse encoding with input validation or SQL injection prevention.

Parameters:

  • $string — A named input or configuration argument; use the declared type and optional default shown above.
  • $flags — A named input or configuration argument; use the declared type and optional default shown above.
  • $encoding — A named input or configuration argument; use the declared type and optional default shown above.
  • $double_encode — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of htmlspecialchars().

<?php

echo htmlspecialchars("<h1>Hello</h1>", ENT_QUOTES, "UTF-8");

Output:

&lt;h1&gt;Hello&lt;/h1&gt;

The output demonstrates the primary operation: escape the HTML-special characters in a string.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of htmlspecialchars().

<?php

echo htmlspecialchars("Tom & Jerry", ENT_QUOTES, "UTF-8");

Output:

Tom &amp; Jerry

Compared with the first example, this one illustrates the same operation with a different input shape or option. Escape at output time for the correct HTML context. Do not confuse encoding with input validation or SQL injection prevention.

Practical Example: A Real Application Scenario

Here is a practical use of htmlspecialchars() with realistic sample information. Adapt the values to match your application.

<?php

$comment = "<script>alert(1)</script>"; echo "<div>" . htmlspecialchars($comment, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8") . "</div>";

Output:

<div>&lt;script&gt;alert(1)&lt;/script&gt;</div>

This is a useful starting point, but production code should check errors and validate values before performing the operation. Escape at output time for the correct HTML context. Do not confuse encoding with input validation or SQL injection prevention.

Important Behavior and Edge Cases

Escape at output time for the correct HTML context. Do not confuse encoding with input validation or SQL injection prevention.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
htmlspecialchars()Escape the HTML-special characters in a string.
Related functionshtmlentities() converts additional characters; htmlspecialchars_decode() reverses selected entities.

Common Beginner Mistakes

MistakeCorrect Approach
Treating HTML escaping as input validationValidate inputs separately and escape for the specific output context.
Decoding untrusted HTML and printing it directlyDecoded markup can become active HTML; escape it again before rendering.
Using HTML encoding to secure a SQL queryhtmlentities() converts additional characters; htmlspecialchars_decode() reverses selected entities.

Key Takeaways

  • htmlspecialchars() lets you escape the HTML-special characters in a string.
  • Escape at output time for the correct HTML context. Do not confuse encoding with input validation or SQL injection prevention.
  • htmlentities() converts additional characters; htmlspecialchars_decode() reverses selected entities.
  • Check input, return values, and environmental requirements when adapting the examples.

htmlspecialchars() provides a focused PHP operation for escape the HTML-special characters in a string. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.