CSI: PHP

"Looking at your tweets I cannot even fathom what your job is. CSI:PHP?" — @grmpyprogrammer

Templating Gone Horribly Wrong

| Comments

Using any form of templating, even if it’s just including a header.php and footer.php in each page, is supposed to make a developer’s life easier. Imagine the confusion the below code causes when, as you’re reviewing some new code, you run across a seemingly random </head> tag. Where is the rest of the markup, you might ask? Why, take a look at doPageHead(), silly.

1
2
3
4
5
6
7
8
9
10
<?php

function doPageHead($lang='en') {

    print "
    <html lang="$lang" xmlns="http://www.w3.org/1999/xhtml" xml:lang="$lang">
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type"></meta>
    ";
}

I’m not a big fan of functions that simply echo HTML, but if you’re going to do something like this, at least include the closing head tag. Seriously. Or I might come find you and punch you square in the jeans.

Another, better option is to find a PHP templating engine that’ll do most of your templating for you.

Comments