To directly answer your question: Never echo static text or HTML. Leave that outside of your PHP.
<?php echo "<p>Hello World</p>"; // Alway wrong. ?>
<p>Hello World</p> <!-- Does the same thing, faster, and is more readable. -->
Escaping to PHP is a waste if all you are going to do is pass in static text or static html. All code inside of <?php ?>
is sent to PHP to be interpreted.
This means your website is slower and you are wasting electricity if you are passing large amounts of static text to PHP for no reason. (Sometimes there is a reason.)
Best practice is to have your logic and grunt work in files without any HTML, only PHP.
The way I like to do this is with an MVC framework. I have pure PHP in the models and controllers, and a mixture of HTML and PHP in the views. You don't need an MVC framework; you could just stick your mixture of HTML in template files, which you then include in your pure PHP files.
Once you have your variables (which don't include HTML in them) created in pure PHP files, you can pass them off to the file with your HTML. There, and only there, is it fine to mix HTML and PHP. But, and this is an important but, when working with files that have HTML, keep the PHP to a minimum.
Luckily, PHP comes with friendly tools to make PHP easier to read when mixed with html.
Alternative Syntax
The best practice is to use regular PHP syntax when writing pure PHP, but the alternative syntax, when mixing PHP and HTML.
In regular PHP files without any HTML:
if ($something) {
do_something(); // do_something() shouldn't echo or print.
}
This is beautiful for pure PHP, but not ideal when escaping from HTML. In particular, the closing braces are often hard to understand when they show up in big piles of HTML.
It seems, based on your question that you would have written, in a file mixing HTML and PHP, this:
<?php
if ($something) {
echo '<p>Hello World.</p>';
}
?>
For the reasons described above, we want the HTML not to be sent to PHP. We do this like so:
<?php if ($something) { ?>
<p>Hello World.</p>
<?php } ?>
We got our HTML out of our PHP tags, which is an improvement. But we could go further.
Notice how uninformative the closing brace is? We don't know if it is closing a loop or an if statement. This seems trivial in the above example, but there are often dozens of lines of HTML inside of if/endif inside of a loop/endloop.
The best practice is that in a file mixing PHP and HTML, you use the alternative syntax, which omits opening and closing braces ({ }
):
<?php if ($something): ?>
<p>Hello world.</p>
<?php endif; ?>
Aside from the semantics of the new syntax (:
and endif;
instead of {
and }
), you should notice two things:
- Again, the HTML and text, while controlled by PHP, is outside of the
<?php ?>
tags, so it is not interpreted. For the reasons described above, this is good.
- This is much more informative than if this were all php including random closing braces (
<?php } ?>
) closing things that are hard to find where they opened (because they are mixed with a buch of messy looking HTML). Again, imagine a big HTML file with loops and if statements. Specifying what you are closing can clarify a lot.
The same template friendly syntax can be used for loops as well:
<?php foreach ($things as $thing): ?>
<li><?php echo $thing; ?></li>
<?php endforeach; ?>
Short Tags
Another great tool provided by PHP which, again, you only want to use when mixing PHP and HTML together, is short tags. You might have to turn short tags on in php.ini file. Once turned on, short tags make everything even more readable.
Without short tags, you have to write <?php echo $something; ?>
but with short tags you can write <?=$something?>
and it will have the exact same result.
When combining short tags and the alternative PHP syntax, you can really write some elegant HTML files, which include PHP elements. Taking our last loop example (just the part inside the loop), short tags allow us to reduce:
<li><?php echo $thing; ?></li>
to
<li><?=$thing?></li>
which is much more readable.
Could we go further? Yes. Using various templating systems you can do things like:
<li>{thing}</li>
Which is amazingly readable. Templating systems, however, are way outside the scope of the original question (which I left a while ago anyway).