Format Code Now
/
Web & markupHTML to PHP Converter Next: Table to HTML Converter ›

HTML to PHP Converter

WM-68

Paste HTML and get PHP code that prints exactly that HTML. Choose one echo per line, one single-quoted string, or a nowdoc block that needs no escaping.

Runs in your browser. Nothing is uploaded.

HTML

PHP

How to use it

  1. Paste your HTML into the input box.
  2. Choose Echo lines, Single-quoted string or Nowdoc block.
  3. Set the variable name for the string and nowdoc outputs, and whether to add the <?php tag.
  4. Copy the PHP or download it as a .php file.

The three output styles

Echo lines writes one statement per line, such as echo '<p>Hi</p>' . "\n";, so the output keeps your line breaks. Single-quoted string puts all of the HTML in one variable. Nowdoc block writes <<<'HTML' followed by your text and a closing HTML; marker. A nowdoc does not interpolate variables and needs no escaping, which makes it the cleanest choice for long markup. It needs PHP 5.3 or newer.

What gets escaped

In the two quoted styles the text is placed in single quotes, where PHP only treats the backslash and the single quote as special. The tool escapes both, so a $ sign stays a literal dollar sign and nothing is interpolated. In the nowdoc style nothing is escaped. If your HTML already contains the word HTML, the closing marker becomes HTML_1, HTML_2 and so on, so it cannot end the block early.

What it does not do

The tool does not parse, validate or change the HTML. It only wraps your text, and it normalizes line endings to \n. It does not check that the PHP runs, and it does not escape output for use inside another PHP string or for use in an attribute. If you want values from variables inside the markup, you will need to write that part yourself.

Error messages and what to do

MessageWhat it meansFix
Paste some HTML to wrap in PHP.The input is empty or holds only whitespace.Paste the HTML you want to wrap.

Questions people ask

How do I convert HTML to PHP?

Paste the HTML and pick a style. Echo lines gives one echo per line, the string style gives a variable holding the markup, and Nowdoc gives a heredoc-like block with no interpolation. Copy the result into your PHP file.

Should I use echo or a nowdoc?

Use a nowdoc for long blocks, because nothing needs escaping and the HTML stays readable. Use echo lines when you want to build output piece by piece, or when you are on a PHP version older than 5.3.

Why does the output use single quotes?

Inside single quotes PHP does not look for variables or escape sequences, except for the backslash and the single quote. That makes the escaping simple and predictable, and dollar signs in your markup are safe.

What is the difference between nowdoc and heredoc?

A heredoc interpolates variables like a double-quoted string. A nowdoc does not, like a single-quoted string. This tool writes a nowdoc so the markup is printed exactly as you pasted it.

Does it add a closing ?> tag?

No. Files that contain only PHP are better without the closing tag, since a stray space after it would be sent to the browser as output.

About this tool

This does not parse or change the HTML: it wraps your text as it is, so the PHP prints exactly what you pasted. Echo lines writes one echo statement per line, in single quotes, with a "\n" added after each line. Single-quoted string puts everything in one variable, where only the backslash and the single quote need escaping, so $ signs stay literal. Nowdoc block writes <<<'HTML' ... HTML; which needs PHP 5.3 or newer and escapes nothing; if your text already contains the word HTML the tool picks HTML_1, HTML_2 and so on so the closing marker is unique. Line endings are normalized to \n. The opening <?php tag is optional, and the variable name for the string and nowdoc outputs comes from the list. It does not check that the HTML is valid or that the PHP runs, and it does not add escaping for output inside an existing PHP string.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.