You can use the <<< sequence like this (this is called a Heredoc string):
$HtmlOutput = <<<_END
<form method="GET">
<div>Search Term: <input type="search" id="q" name="q"></div>
<div>Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25"></div>
<input type="submit" value="Search">
</form>
_END;
$HtmlOutput = "";
$HtmlOutput .= <<<_END
_END;
The terminating marker must be at the start of a line and the only thing on the line (no comment or even whitespace after it!!!)
The advantage of this is that there is no need for line breaks, escape characters (\’ etc) – what is between the markers is output exactly as it appears but with any variable names evaluated. _END can be anything. You can also use &MyText <<< _END … to assign the contents to a variable instead of echoing.
You can include variables within the text. For complex variables you can enclose them in curley brackets like this:
blah blah {$foo->bar[1]} blah
Using with echo
You can do this:
echo <<<_END
_END;
or this:
$HtmlOutput = "";
$HtmlOutput .= <<<_END
_END;
echo($HtmlOutput);
Including a function call
${!${''} =(time())}
$HtmlOutput = <<<_END
<p>Somthing ${!${''} =(time())} someting</p>
_END;
(This is based on the topic here and relies on this nifty parser hack)
Using newline “\n” etc in a Heredoc string
Any \n inside double-quoted JS strings will be interpreted as real newlines, and sometimes break the heredoc or cause PHP to think you are escaping things.
Use NOWDOC instead of HEREDOC
No interpretation happens inside a NOWDOC:
\n is not turned into newlines
$variables are not expanded
Backslashes are not special
Perfect for embedding JavaScript/HTML templates
$HtmlOutput .= <<<'_END'
_END;
If you must use HEREDOC (<<<HTML) instead of NOWDOC
You must escape backslashes so \n becomes \n.
$HtmlOutput .= <<<_END
<script>
console.log("This will not break: \\n \\t");
</script>
_END;
This can get messy fast though :-)
