Directory structuring

When it come to choosing how to lay out your directories you will find it usually comes down to personal preference. Yes, use directories because sorting your scripts into logical groups makes maintenance much easier, but remember that you will need to work a little harder to maintain links between files. When it comes to handling includes and links, you will probably find that absolute links work better than relative links as they allow you to use a standard header/footer include code for your pages, like this:

<?php
    include '/home/paul/public_html/header.php';
    // your page here
    include '/home/paul/public_html/footer.php';
?>

Using absolute links as above means you can take that PHP code and put it into any script on your site and it will still get header.php and footer.php correctly. Note that you will also need to absolutely link to images in your header and footer, like this:

<IMG SRC="/pics/logo.jpg" />

The source of the image starts with a forward-slash / so that the web browser looks to the root of the web server, e.g. www.example.com/pics/logo.jpg as opposed to www.example.com/somedirectory/someotherdirectory/pics/logo.jpg.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Group development >>

Previous chapter: Laying out your files

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.