Dynamic websites with PHP (part 2)

OK, what more do I need to know?

Well, working with files on a server using PHP is a little different from working with files on a server using HTML. PHP is a relatively simple yet very powerful scripting language and unlike HTML, it can be used to access files above the web root. This superpower means that file paths need to be dealt with differently.

So there's a catch?

Yes but it's not a big deal. Here's the thing — with HTML, we can point to any folder below web root using the slash character like this /folder. If we try the same technique with PHP, it doesn't work because PHP can see a few levels above web root and so we need to add those other folder levels to our path.

How can I find out what those folder are called?

Usually, you will need to contact your web host because the upper levels may not even be visible using FTP. It usually looks something like this: /home/account_name/public_html/folder/ (where public_html is the web root). But in practice, this will vary from one host to another and depends how the server is configured.

This is all sounding rather complicated

Yes, it is but don't despair because PHP can rescue us from this uncertainty. It stores the path from the server account root to the web root in a superglobal variable (see, I told you PHP has super powers). The variable is called $_SERVER['DOCUMENT_ROOT'] and it can be used in place of the bit of the path we don't know.

That is really useful, how do we use it

Well, in order to provide a full path to the include function, we need to add the variable to the bit of the path we do know. In PHP, we can build a compound string from variable and bits of text using the dot (period) character. This is technically known as “concatenation”.

Let's assume we have a file called footer.inc.php and it's in a folder called includes just below web root. We can build a path to that file from anywhere in our website using this code:

$_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc.php'

The beauty of this approach is that we never need to know what the bit of the path above web root is called and (even better) if we ever move our site to a different host that uses a different configuration, our site will not break because the super global variable always contains the correct information.

OK, I'm beginning to like this PHP business

PHP is written with ease of use in mind and with a little practice, you should find it quite easy to build modular websites.

So, show me the full include thingy

OK, so we want to include the footer.inc.php file in our homepage. All we need to do is use the include function as we did before, but use the concatenated string in place of the filename like so:

<?php include ($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc.php'); ?>

And that's all there is to it?

Yes, of course you need to make sure that the syntax is correct; otherwise it just won't work, so pay special attention to all the quotes, semi-colons and parenthesis. Take a look at Chris Coyier's explanation for more background.

That method makes the path quite verbose, can we shorten it?

Well, we can't really shorten it but if a file contains a number of includes, it may be more efficient to set the include path before any include statements are made like so:

<?php set_include_path($_SERVER['DOCUMENT_ROOT']); ?>

This means we can drop the path above web root when we make subsequent include statements, like so:

<?php include('includes/footer.inc.php'); ?>

Notice that there is no leading slash in the path because the set_include_path function has already made sure that we will be starting at web root.