Content · Programming · PHP

PHP htmlentities() Function: Usage and Examples

Learn htmlentities() 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 encode applicable characters into HTML entities. This comes up when preparing data, displaying results, or integrating information with other services.

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

What Is htmlentities() in PHP?

Encode applicable characters into HTML entities.

Its syntax is:

htmlentities(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: Choose an explicit encoding and handle invalid sequences. HTML entities are for HTML output contexts, not database query safety.

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 htmlentities().

<?php

echo htmlentities("<b>Tom & Jerry</b>", ENT_QUOTES, "UTF-8");

Output:

&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;

The output demonstrates the primary operation: encode applicable characters into HTML entities.

Example 2: Understanding Another Case

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

<?php

echo htmlentities("'quoted' & <tag>", ENT_QUOTES, "UTF-8");

Output:

&#039;quoted&#039; &amp; &lt;tag&gt;

Compared with the first example, this one illustrates the same operation with a different input shape or option. Choose an explicit encoding and handle invalid sequences. HTML entities are for HTML output contexts, not database query safety.

Practical Example: A Real Application Scenario

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

<?php

$description = "<Movie> & more"; echo "<p>" . htmlentities($description, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8") . "</p>";

Output:

<p>&lt;Movie&gt; &amp; more</p>

This is a useful starting point, but production code should check errors and validate values before performing the operation. Choose an explicit encoding and handle invalid sequences. HTML entities are for HTML output contexts, not database query safety.

Important Behavior and Edge Cases

Choose an explicit encoding and handle invalid sequences. HTML entities are for HTML output contexts, not database query safety.

  • 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
htmlentities()Encode applicable characters into HTML entities.
Related functionshtmlspecialchars() encodes the essential special characters and is usually sufficient for HTML escaping.

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 queryhtmlspecialchars() encodes the essential special characters and is usually sufficient for HTML escaping.

Key Takeaways

  • htmlentities() lets you encode applicable characters into HTML entities.
  • Choose an explicit encoding and handle invalid sequences. HTML entities are for HTML output contexts, not database query safety.
  • htmlspecialchars() encodes the essential special characters and is usually sufficient for HTML escaping.
  • Check input, return values, and environmental requirements when adapting the examples.

htmlentities() provides a focused PHP operation for encode applicable characters into HTML entities. 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.