Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
HTML

How to Assign a PHP Variable as an HTML Table Caption

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.