PHP include path

I’ve been doing a lot more PHP programming lately, especially integrating Word Press blogs (like this one). It’s been important to me that the blog part of the web site matches the look and feel of the rest of the site.

This means making sure that the style sheet for the main site lives peacefully with the style sheet for the blog. It also means that there are sections of the pages such as the menus that I want to keep in separate files and include them where needed. This makes it much easier to make global changes.

The way that PHP uses the path for includes is confusing, and rather than try to explain the rules, I’m going to show you what worked for me. I’m not enough of a programming geek to always want to know why, sometimes I just want it to work so I can get on with the project at hand.

Here’s a simplified directory structure of a typical site:

/ /index.php /about-us.php /blog/ /blog/myblog.php /includes/ /includes/menu.php

Suppose we want to include menu.php in both /about-us.php and /blog/myblog.php. For /about-us.php it is easy:

include ("includes/menu.php");

But /myblog.php is a bit harder, since you can’t just use ../includes/menu.php like you would think. But this will work: