PHP htmlentities() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, htmlentities() converts applicable characters to HTML entities so browsers show them as text.

The string is required. Flags and encoding are optional. Prefer htmlspecialchars() when you only need the common special characters.

Syntax

Call htmlentities on the string. Start with this line.

PHP
$safe = htmlentities($text, ENT_QUOTES, 'UTF-8');
CallWhat it does
htmlentities("<b>Hi</b>")Encodes angle brackets and more.
htmlspecialchars($text, ENT_QUOTES, 'UTF-8')Encodes a smaller special set.

Always set ENT_QUOTES and UTF-8 when escaping for HTML attributes and text.

Library declarations
PHP
htmlentities(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 encode.
flagsOptional. Quote and entity handling flags.
encodingOptional. Character encoding. Prefer UTF-8.

Return Value

Return ValueDescription
stringA string with characters converted to HTML entities.

PHP Examples

Example 1 — Encode Tags and Symbols

Turn markup characters and © into entities for safe HTML text.

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

How It Works

  1. Specials. <, >, and & become &lt;, &gt;, &amp;.
  2. Symbols. © becomes &copy; (entity coverage beyond htmlspecialchars).
  3. Flags. ENT_QUOTES prepares quote encoding when quotes appear.
  4. UTF-8. Passing "UTF-8" keeps multi-byte characters correct.

Example 2 — Encode Quotes with ENT_QUOTES

Both single and double quotes become entities when ENT_QUOTES is set.

PHP
<?php
echo htmlentities("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. Safer when placing text inside HTML attributes.
  4. Defaults. Pass flags explicitly—PHP 8.1+ changed default flag combinations.

Example 3 — Encode, Then Decode

Matching flags and UTF-8 restore the original string.

PHP
<?php
$original = '<a href="x">Hi</a>';
$encoded = htmlentities($original, ENT_QUOTES, "UTF-8");
$decoded = html_entity_decode($encoded, ENT_QUOTES, "UTF-8");

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

How It Works

  1. Encode. Markup and quotes become entities.
  2. Decode. html_entity_decode() reverses them.
  3. Same flags. ENT_QUOTES on both sides keeps quotes consistent.
  4. Strict match. === confirms an exact round-trip.

Frequently Asked Questions

htmlentities() converts characters that have HTML entity equivalents into those entities. It encodes specials like &, <, and > plus many symbols and accented letters (such as © and é) so they display safely as text in HTML.
string htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true). Pass the input, ENT_* flags, UTF-8 encoding, and whether to encode existing entities again.
htmlspecialchars() encodes only the core specials (&, <, >, quotes). htmlentities() encodes every character that has an HTML entity equivalent—including ©, €, and many accented letters. For typical XSS-safe page output, htmlspecialchars() is usually enough.
Use htmlentities() when you need full entity encoding of symbols and accented characters. For standard HTML escaping against XSS, htmlspecialchars() is the common choice.
When true (default), existing entities like &copy; are encoded again to &amp;copy;. When false, already-encoded entities are left unchanged—useful when input may already contain entities.

Next: htmlspecialchars_decode()

Learn how to reverse htmlspecialchars() encoding.

htmlspecialchars_decode 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