Separating PHP from HTMLVery often PHP coders will output HTML like so: <?php print '<ul>'; foreach( $links as $link=>$title ){ print '<li><a href="'.$link.'">'.$title.'</a></li>'; } print '</ul>'; ?> This causes the following issues: It's harder for web designers to work with It gets in the way of syntax highlighting It gets in the way of code-assist You have to watch out for quotes. So what's the answer. Well here's how I do it.
<ul> <? foreach( $links as $link=>$title ){ ?> <li><a href="<?=$link;?>"><?=$title;?></a></li> <? } ?> </ul> This approach involves a few more PHP tags but it solves all of the above issues. It works better in IDEs, is easier to read and maintain and helps keep PHP and HTML separate. 06/06/2009 permalink | Posted in web development | 3 Comments » Leave a reply |
About meAdam Jimenez is a freelance web developer who has been professionally developing websites since 2000.Find me
Projects
Archive |