HTML to PHP
Wrap HTML in PHP echo statements, variables, or heredoc.
HTML to PHP Converter
The HTML to PHP converter wraps HTML markup in PHP output statements so you can drop it into a function, a WordPress template, a plugin, or any PHP script. Escaping quotes by hand is error-prone — one missed apostrophe and the page throws a parse error. The converter escapes everything correctly for the string style you choose.
Output styles
- echo per line – each HTML line becomes its own
echo '…';statement. Easy to read and edit line by line. - Single echo – one
echowith a multi-line string. - String variable – builds the markup with
$html = '…'; $html .= '…';and echoes it at the end, ideal when a function should return HTML (for example a WordPress shortcode). - Heredoc –
echo <<<HTML … HTML;, convenient for large blocks;$signs are escaped so they print literally. - Nowdoc –
<<<'HTML', which never interprets variables and needs no escaping at all.
Quotes and escaping
With single quotes, only apostrophes and backslashes are escaped — HTML attribute double quotes stay untouched, which keeps the code clean. With double quotes, the converter escapes ", $, and backslashes and adds \n so the generated page source keeps its line breaks. You can rename the variable and choose whether to wrap everything in <?php … ?>.
Inserting dynamic values
After converting, replace static values with PHP variables. In single-quoted strings, concatenate: '<h1>' . esc_html( $title ) . '</h1>'. In heredoc, use {$title} — but always escape user data with htmlspecialchars() or the WordPress esc_* functions to prevent XSS.
Related tools
Convert HTML for React with HTML to JSX, tidy the markup first with the HTML Beautifier, or escape single strings with the HTML Escape tool.
Frequently Asked Questions
Should I use single or double quotes?
Single-quoted strings are printed literally, which is safest for HTML full of double quotes. Double-quoted strings interpret variables and escapes such as \n, so the converter escapes $ and " for you and adds newlines.
What is the difference between heredoc and nowdoc?
Heredoc (<<<HTML) works like a double-quoted string and expands variables; dollar signs in your HTML are escaped so they print literally. Nowdoc (<<<'HTML') never expands anything, so the HTML is pasted exactly as is.
Do I need echo at all?
Not necessarily. In a .php file you can close PHP with ?> and write plain HTML. Converting is useful when the HTML must be built inside a function, a class, a loop, or returned as a string.