FormatterHQ

HTML to PHP Converter

Wrap HTML in PHP code: echo statements, concatenation, a string variable, heredoc or nowdoc. Quotes, backslashes and dollar signs are escaped for you.

Input HTML
Output PHP
Ready.

How to use the HTML to PHP Converter

  1. Paste the HTML you want to output from PHP.
  2. Choose the output style: echo per line, concatenation, variable, heredoc or nowdoc.
  3. Pick single or double quotes (and a variable name for the variable style).
  4. Convert and copy the PHP code into your script.

Putting HTML inside PHP safely

When HTML has to be produced from PHP code (in a plugin, a legacy script or a helper function), every quote and dollar sign in the markup can break the string or trigger unwanted variable interpolation. This HTML to PHP converter wraps your markup in valid PHP and escapes exactly the characters that need it for the style you choose.

Output styles compared

For the input <span>$19.99</span> it's a deal:

// echo (single quotes)
echo '  <span>$19.99</span> it\'s a deal' . PHP_EOL;

// echo (double quotes)
echo "  <span>\$19.99</span> it's a deal\n";

// variable
$html .= '  <span>$19.99</span> it\'s a deal' . PHP_EOL;

// heredoc
echo <<<HTML
  <span>\$19.99</span> it's a deal
HTML;

// nowdoc
echo <<<'HTML'
  <span>$19.99</span> it's a deal
HTML;
  • Echo – one echo per line. Easy to read and to edit line by line.
  • Concatenation – a single echo statement joined with ., which keeps the output in one expression.
  • Variable – builds the markup in a variable (default $html) with .= and echoes it at the end, handy when a function should return HTML instead of printing it.
  • Heredoc – keeps the HTML almost untouched between <<<HTML and HTML;. Variables inside heredoc are interpolated, so literal $ signs are escaped.
  • Nowdoc – like heredoc but with a quoted label (<<<'HTML'); nothing is interpolated, so no escaping at all is needed.

Single vs double quotes

In single-quoted PHP strings only \' and \\ are special, so the converter escapes those and adds PHP_EOL to keep line breaks. In double-quoted strings, ", \ and $ are escaped and each line ends with \n. Single quotes are slightly simpler and avoid accidental interpolation; double quotes let you insert variables later.

If a line in your HTML happens to equal the heredoc label, the converter automatically picks a different label (HTML_), so the block never ends early.

When to use which

  • Short snippets inside logic: echo or concatenation.
  • Functions that return markup: variable.
  • Large static blocks: nowdoc, because it is the most readable and needs no escaping.
  • Large blocks where you will add {$variables}: heredoc.

A better long-term approach

For anything beyond small snippets, a template (a Blade or Twig view, or a plain PHP template file where HTML lives outside <?php ?> tags) is easier to maintain than HTML inside strings. And whenever you insert user data into HTML, escape it with htmlspecialchars() to prevent cross-site scripting. You can see what that escaping does with the HTML escape tool.

Tidy the markup first with the HTML formatter and the PHP result afterwards with the PHP formatter. Conversion happens in your browser.

Frequently asked questions

What is the difference between heredoc and nowdoc?
Heredoc works like a double-quoted string, so variables such as $name are interpolated and literal dollar signs must be escaped. Nowdoc works like a single-quoted string: nothing is interpolated and no escaping is needed.
Why does the output contain PHP_EOL?
Single-quoted PHP strings do not understand \n, so the converter appends PHP_EOL to keep the line breaks of your HTML. Double-quoted output uses \n instead.
Can I insert PHP variables into the generated code?
Yes. With double quotes or heredoc, write {$variable} where you want the value. Remember to escape user data with htmlspecialchars() before outputting it.
Does the converter change my HTML?
No. Only the characters PHP needs escaped are changed; the markup itself, including whitespace, stays as you pasted it.

Last updated October 3, 2026

Related tools

cURL to PHP Converter Convert curl commands into PHP code using the curl_* functions: method, headers, body, auth, cookies and SSL options. JSON to PHP Array Converter Convert JSON to a PHP array literal with short [] or array() syntax, ready to paste into Laravel config files, seeders and tests. BBCode to HTML Converter Convert forum BBCode such as [b], [url], [quote], [list] and [code] into clean, safe HTML markup instantly. CSV to HTML Table Converter Convert CSV to HTML online for free. Get clean table markup with a live preview. Excel to HTML Table Converter Convert Excel to HTML online for free. Get clean table markup with a live preview. HTML Entities to Text Turn HTML entities (named, decimal and hex) back into plain Unicode text. HTML to Pug Converter Convert HTML to Pug template syntax: indentation-based tags, #id and .class shortcuts, attributes and text blocks. HTML to Text Converter Extract readable plain text from HTML: keeps paragraphs, headings, lists and tables, drops tags, scripts and styles. Hyperlink Generator Generate a hyperlink as HTML, Markdown, BBCode and plain text at once - ready for websites, READMEs, forums and e-mails. Jade to HTML Converter Render legacy .jade templates to HTML and find the deprecated Jade syntax to fix before moving to Pug or plain HTML. Mailto Link Generator Create mailto: links with pre-filled recipients, CC, BCC, subject and body — correctly URL-encoded, with the HTML <a> tag. Marquee Generator Generate HTML marquee code for scrolling text with direction, speed, behaviour, colours and pause-on-hover, with live preview.