Creating Dynamic Websites: Start Small with PHP Includes

Author of this post: Karen Morrill-McClure | About Blog Authors »

php-includes-web.jpg

AJAX, Web 2.0, Ruby on Rails, and a host of other amusing acronyms and phrases are the current buzzwords in web design. Today’s cutting edge sites use these server platforms and programming languages to make web sites jump through hoops for users. What does that mean for Jane Web Designer, who’s serving Jane Business Owner, down in the trenches?

It’s good news actually, because the technology that powers the high-profile dynamic sites (think Amazon.com) is tried and true, and it’s available to everyone with an $8 a month web hosting account.

If coding your own content management system or online shopping cart sounds like a daunting task, I suggest getting your feet wet with PHP includes. We’ll deal with mySQL in a separate post.

Though they’re not the most glamorous bits of code in the world, PHP includes do make life a lot easier. Suppose I have a site all coded and just about ready to launch and the client says, ‘Hey, we’re not ready with the information for page x, can we pull it from the site?’ Do I tear my hair out because I have to change the HTML for the navigation on every page of the site? No. I make one change in one file, upload that changed file, and I’m done.

That file I changed contains just the code for the navigation, and I use PHP to include that code on every page of my site where I want the navigation to go.

So, how can you harness the power of includes?

First, you’ll need to create PHP files instead of HTML files. Luckily, that’s easy to do - just give your files a PHP extension instead of a HTML extension. Then, create the include file. It should contain only the HTML code for your navigation.
Finally, you need this line of php in your page files where you want the navigation to go: < ? include('inc-nav.php'); ?>. ‘inc-nav.php’ is the name of your navigation include file.

The include command points to a file just like an HTML tag does, so if the include file is in a sub-folder, you’ll need to put the path in front of the file name. Other than that, you’re all set.

I do all my navigation in includes. It saves time and prevents small, stupid errors, like forgetting to take a button off one page of the site. Whenever I’m starting a site, I look for pieces of code that appear on multiple pages and consider putting that code in includes.

I’ve put donation forms in includes and then styled them differently depending on what page they are on using CSS. If I need to change the form, I know that the change will propagate across the site. I also use includes with sites where I’m checking in and checking out pages through Dreamweaver, which lets other people work on the same site without overwriting my work. If I have to change the navigation, I don’t have to check out the entire site, make the change, and check it back in. I just change that single include file.

Includes are a good way to dip your toe into the PHP waters. Who knows, once you see how easy includes are, you may want to move on to blogs, content management systems, and online portfolios. The possibilities are endless.

10 Responses to “Creating Dynamic Websites: Start Small with PHP Includes”

  1. AliKat Says:

    Make sure when using links in subfolders or folders other than the one your navigation file is in that they will all point to the correct file. This generally means that you will have to hard code the full url path to each page into your navigation links in some form or another. One good way is to include your url path in a variable and make all the links from there direct as if from the root directory.

  2. Jesse Nivens Says:

    Very helpful article: I think many graphic designers who are just beginning the transition to Web are afraid of PHP. But this shows how easy it is to get started.

    I would also recommend (as the article touches on) always saving web files as PHP files rather than HTML, even if you don’t know how to use PHP yet… because you will learn something like this suddenly and unexpectedly, and then regret the fact that you have to change the names/links/etc on dozens or hundreds of files.

  3. grassy Says:

    how i can enable remote php include in server configuration?

  4. Search Engine Optimization Says:

    abosutely its great to learn the dirty work because everything lese becomes natural

  5. favorites Says:

    Thank you for this post. Very interesting

  6. Seo blog Says:

    I would also recommend (as the article touches on) always saving web files as PHP files rather than HTML, even if you don’t know how to use PHP yet… because you will learn something like this suddenly and unexpectedly, and then regret the fact that you have to change the names/links/etc on dozens or hundreds of files.

  7. Spirit69 Says:

    Very helpful article: I think many graphic designers who are just beginning the transition to Web are afraid of PHP. But this shows how easy it is to get started.

  8. gras Says:

    I would also recommend (as the article touches on) always saving web files as PHP files rather than HTML, even if you don’t know how to use PHP yet… because you will learn something like this suddenly and unexpectedly, and then regret the fact that you have to change the names/links/etc on dozens or hundreds of files.

  9. kanser Says:

    I think many graphic designers who are just beginning the transition to Web are afraid of PHP. But this shows how easy it is to get started.

  10. Tatiana Says:

    great post hope to see some additional comments here…:)

Leave a Reply

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Self-Help Art
July 9th, 2008
Inspiration Art