I’ve seen HTML nested within a PHP page written in so many ways… some of which are demonstrated in this page:
http://austingulati.com/2010/01/many-ways-to-integrate-html-into-php/
But… I very rarely see HTML and PHP written in unison as follows:
echo <<<EOF
<h1>Welcome</h1>
<p>Hello</p>
<p>{$somePHPVariable}</p>
EOF;
Question:
Is there a fundamental issue with using the EOF approach that I should be aware of?
Heredocs are wonderful if you’re building HTML and need to embed variables.
They honor the linebreaks/spacing you embed within them (even if the browser doesn’t display that) so it’s MUCH easier to build nicely formatted HTML, and also relieve you of the need to escape quotes while building the strings:
e.g. compare
v.s.
They can also be used in concatenation operations, eg.
will eventually give $x the value
hello there, how are you?. Basically consider the heredoc syntax to be a VERY fancy version of double-quoted strings, with none of the drawbacks. The only restriction is that the heredoc’s sentinal value must be on a line by itself, so there’s no way to make a “one line” heredoc.