You enter <strong>Read the lesson</strong> as a task title. Back in the list, the brackets have vanished and the words are bold. PHP printed the string, but the browser treated it as HTML.
That boundary is why our task-list lessons use htmlspecialchars(). We want a title to appear as text inside HTML, without letting its author change the surrounding page structure. Start with a harmless bold-text experiment so the difference is easy to see.
Did we send text or HTML?
Create experiment.php in a new folder using the fixed example below. No data needs to be submitted to a remote server. Run php -S 127.0.0.1:8001 in that folder and open http://127.0.0.1:8001/experiment.php.
<?php
$title = '<strong>Read & try</strong>';
?>
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Raw output experiment</title>
<p><?= $title ?></p>
</html>
The browser displays Read & try in bold. It recognizes <strong> as markup. PHP's short echo syntax <?= ... ?> does not automatically encode a value for HTML; it inserts the string into the response.
The raw-output experiment uses only a fixed string we wrote ourselves. Do not use that raw pattern for visitor input. Replace the entire file with this version:
<?php
$title = '<strong>Read & try</strong>';
?>
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Escaped output experiment</title>
<p><?= htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></p>
</html>
After refreshing, the whole <strong>Read & try</strong> string is visible. We did not remove the tags; we made them display as text. View the page source and you will see < instead of the less-than sign, > instead of the greater-than sign, and & for the ampersand. The browser renders these references as text characters.
Read the three arguments
The first argument is the string to encode: $title. ENT_QUOTES asks for both single and double quotes to be encoded. ENT_SUBSTITUTE replaces invalid character sequences with a replacement character. The | between them is bitwise OR, combining flags; it is not the logical || used in conditions.
The final argument, 'UTF-8', specifies how to interpret the input's character encoding. This is not a tool that translates every accented letter into an HTML entity; it handles characters with special HTML meaning. The actual text encoding and the page's meta charset should agree too.
Quotes matter inside an input value
<?php
$title = 'Read "PHP" & try';
?>
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Attribute experiment</title>
<input value="<?= htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?>">
</html>
The input should contain the complete Read "PHP" & try title. We enclose the HTML attribute in double quotes, then encode quotes inside the value so they are not interpreted as the attribute's closing delimiter. Quoting the attribute and encoding its value work together.
These examples target HTML text and an ordinary quoted HTML attribute. Do not assume the same function is enough for embedding data into JavaScript, producing CSS, or deciding whether a URL is acceptable. Encoding an href value for HTML, for example, does not validate its URL scheme.
Should the database store the encoded title?
We generally store a validated title as plain text and encode it when rendering HTML. Encoding it on save and again on display can make unexpected pieces such as & appear. Run this next experiment in the terminal:
<?php
$raw = 'Read & try';
$once = htmlspecialchars($raw, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$twice = htmlspecialchars($once, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
echo $once, PHP_EOL;
echo $twice, PHP_EOL;
The first line is Read & try; the second is Read &amp; try. The second call encodes the ampersand produced by the first one. Distinguish response source from rendered text here: in the terminal, we are looking directly at the output characters.
htmlspecialchars() does not make an SQL query safe. Use parameterized queries when passing values to SQL and HTML output encoding when rendering text in HTML. One does not replace the other. If visitors are intentionally allowed to write formatted HTML, that needs a separate approach that checks allowed markup, rather than printing arbitrary input.
Put the long expression in a small helper
This complete file works on its own. We call the helper h(); it is our function, not a special PHP command. Both the paragraph and the input use the same unencoded title.
<?php
declare(strict_types=1);
function h(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
$title = 'Read "PHP" & try';
?>
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Task title</title>
<p><?= h($title) ?></p>
<input value="<?= h($title) ?>">
</html>
The paragraph and input should show the same title. On your computer, change the title to <em>New task</em>. The tags should remain visible as text in both places. View the page source to check that the attribute delimiters and encoded characters are still where they belong.
Where this appeared in the task list: PHP Lesson 01 — Run PHP Locally and Open Your First Page
Official documentation: htmlspecialchars · echo · SQL injection and prepared statements