PHP is often used to generate small “snippets” of content, often through the echo function. Just where and how echo is used is a matter of both efficiency and (to a lesser degree) personal style.

One option is to echo both the content and the HTML surrounding it:

<?php echo '<p>Some dynamic content here</p>' ?>

…or you could place the HTML code outside the , which is usually my preferred solution, especially when combined with a PHP shortcut for echo:

<p><?='Some dynamic content here'?></p>

Neither approach is “wrong”: your chosen solution depends on which one is more efficient.

There is one final consideration we might make. PHP runs without any awareness of the HTML code before or after it; because of that, the code it generates, while perfectly valid, may not be formatted terribly well when you use "View Source" in your browser. If this is important to you, use "escape characters" in your code:

<?php echo '<p>Test content here</p>\n' ?>

That \n forces PHP to create a new line after it executes, making your code more readable. Escapes may also be used before characters that might otherwise be interpreted incorrectly: for example, if in the above line, it we wanted to say “Test text's here” we would alter our PHP code to:

<?php echo '<p>Test text\'s here</p>\n' ?>

In this case the backwards slash character tells PHP that that apostrophe is “real” and is not a single quote that will terminate our code prematurely.

Interweaving , HTML and CSS is a very powerful technique, but you must be clear on where each begins and ends in order to produce a whole, complete and valid page.

Enjoy this piece? I invite you to follow me at to learn more.