I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

PHP echo

php / functions

Estimated reading time: 1 minute, 25 seconds

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 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 , and 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.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.