PHP Tip: Don’t use Echo to Code XHTML
Here is a simple tip for anyone new to PHP (Hypertext Preprocessor) programming on the Web. When you go to output XHTML (or HTML, for that matter) don’t use the echo function. For example, the short-sighted way to do this would be:
echo "<p>Your $variable1; makes me want to go straight home</p>";
You should be able to see that varying amounts of complex XHTML, including backslashes throughout all the extra syntax of XHTML attributes, is going to rapidly become harder to maintain. Instead what you should be doing is closing your PHP block at an appropriate point and then outputting XHTML as it’s born and raised. If you’ve got a variable to output just insert a PHP block in there to call the variable:
<p>Your <?php echo $variable1; ?> makes me want to go home.</p>
The main reason for doing this is because the XHTML rapidly becomes unmaintainable if you’ve got to deal with all of the extra syntactic debugging involved with PHP. So, don’t go there. Continually outputting XHTML as PHP echo statements is something I’d consider a novice’s habit, and one that should be cast aside at the earliest point.
A small tip but hopefully one that might save a little grief for someone, somewhere. In short, place PHP variables into XHTML and not the other way around.


