Place the PHP output expression inside the table’s <caption> element. Keep <caption> as the first child of <table>, and escape the value when it can contain user-supplied text.
Basic pattern
HTML defines <caption> as the title or description of a table. It belongs inside the table element and should be its first child, as documented by the W3C HTML Wiki and the MDN Web Docs mirror.
<?php
$caption = 'Quarterly sales';
?>
<table>
<caption><?php echo htmlspecialchars($caption, ENT_QUOTES, 'UTF-8'); ?></caption>
<tr>
<th>Quarter</th>
<th>Total</th>
</tr>
<tr>
<td>Q1</td>
<td>$12,000</td>
</tr>
</table>
PHP evaluates the expression and writes its result exactly where it appears in the HTML. The browser then receives ordinary HTML with the resulting caption text.
Why escape the variable?
htmlspecialchars($caption, ENT_QUOTES, 'UTF-8') converts characters such as <, >, and quotation marks into safe HTML entities. Use this when the caption comes from a form, database, URL, or any other source that users might influence. A fixed, developer-controlled string does not carry the same injection risk, but consistent escaping is still a useful habit.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Alternative output syntax
You can use PHP’s short echo tag instead of echo:
<caption><?= htmlspecialchars($caption, ENT_QUOTES, 'UTF-8') ?></caption>
This requires short echo tags, which are enabled by default in modern PHP versions. The longer form remains explicit and is suitable when code must accommodate older or unusually configured environments.
Rank #2
Placement and common mistakes
- Put
<caption>directly inside<table>, not outside it. - Place the caption before table rows, row groups, and header or data cells.
- Do not wrap the PHP expression in quotes unless you intentionally want quote characters displayed.
- Escape dynamic text for the HTML context; SQL escaping or URL encoding is not a substitute.
What is known about the SitePoint discussion
The exact SitePoint thread named in the question could not be verified. Related discussions show PHP-generated table markup and dynamic values, including “Help with tables please – PHP” and “CSS for a PHP table”, but they do not establish the original thread’s accepted answer, code, or PHP version. The implementation above follows the HTML specification and applies standard safe-output practice rather than claiming to reproduce that missing post.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




