#11: Working Modularly with PHP

Many sites do not use any fancy Content Management System (CMS) to generate their pages, they are just just good ol' static HTML content. A site for your grandmothers pie baking business probably only has a few pages, perhaps a homepage, about page, and contact page. Each of these pages has different main content, but much of it stays exactly the same, like the header, navigation, and footer. Think of these sections as "modules" that you can easily use PHP to insert into the page. This way, making changes to those sections changes all the pages at the same time, saving you time and potentially mistakes. At the end, I show you how you can still achieve unique effects in your navigation despite the code being identical.


  1. User Avatar
    Permalink to comment#

    Isn’t is better to use ‘if-else’ instead of that ultra-complex css?

    Love your tutorials ;)

  2. User Avatar
    Permalink to comment#

    Fantastic tutorial Chris! A new breakthrough in the way I think about designing websites!

  3. User Avatar
    Permalink to comment#

    Thanks for the great tutorials! I just used this one on my website…

  4. User Avatar
    Permalink to comment#

    Yes you can tell (at 6:10 of the video) that the html code was inserted via php. The way you indent and space your code is unique and you wouldn’t make a simple html indentation mistake like that had it been in an html file and not included using php. A way to fix that, depending on how your organizing your code, would be to use the same spacing as you would a normal html file. So your paragraph tag would a have a couple tab indentations in the header.html file, but your heading tag is fine because php includes the file right where you put the php include statement with the spacing.
    I hope this makes sense. I’m enjoying your screencasts very much! Keep it up Chris.

  5. User Avatar
    Permalink to comment#

    is no longer available for download from the itunes podcast could be reviewed, Greetings

  6. User Avatar

    Instead of that css trouble of selected page, you can add this snippet for printing the menu.. much easier, and then it will have “selected” as class on the page its on..

     "Home","about.php" => "About", "contact.php" => "Contact");
    echo "";
    foreach($pages as $key => $value){
        if($current_page == $key){
            echo " <a href='$key' rel="nofollow"> $value </a>";
            echo " <a href='$key' rel="nofollow"> $value </a>";
    echo "";

    pastebin : http://pastebin.com/23ck7PKk

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.