First off, consider using [man]require[/man] or [man]require_once[/man] rather than [man]include[/man]. Unlike include, require will cause a fatal error if the file you are looking for is not found.
As for the path issue, I think I understand your confusion. If I go visit a script on my website at http://example.com/index.php then inside that file, there is the idea of the current working directory (CWD) which determines the include_path. The CWD and include_path concepts are fundamental to every operating system. The basic idea is that in a PHP script, the web server launches your script with a particular directory in mind as your CWD. You can find out what this is with the function [man]getcwd[/man]. You can set it to something else with [man]chdir[/man]. If you have ever used the linux command line, these functions should remind you of pwd and cd, respectively. In the example above, the CWD is probably something like /var/www/html. If I visit a script at a different url that has some path bits to it, such as http://example.com/my/file/path/index.php, then the CWD will be different -- probably something like /var/www/html/my/file/path
The include_path in PHP is a list of directories that PHP will search when you include, require, or otherwise reference a file. You can find out this value with the function [man]get_include_path[/man]:
What results is a list of paths separated by colons in a linux machines and semicolons on a windows machine (note that PHP defines the global constant DIRECTORY_SEPARATOR as either colon or semicolon accordingly).
When you include or require a file, PHP will search those paths in order until it finds the file you specified -- NOTE that it does so by appending the path and file you have specified to each path in turn. When it finds one, it goes with it.
How does one deal with this in a PHP project that spans many folders? I can think of 3 days:
1) Have every single file in your project require some file named constants.php or something that defines the root of your project like so:
or perhaps like this:
define("PROJECT_ROOT_PATH", realpath(dirname(__FILE__))); // sets it to whatever directory contains the PHP file in which this code exists
drawback: If you wanted to include this file from /var/www/games/games.php, then games.php would need to know where to find constants.php:
// in games.php
require_once "../constants.php"; // we use a relative reference
// then you can do this:
require PROJECT_ROOT_PATH . "/some/other/file.php";
As long as the relative location of games.php and constants.php is preserved, this will always work. Defining this constant is usually a good idea.
2) Have every access to your site go through a single file, usually index.php
In this strategy, the CWD is always the same, because every single http access to your site always goes through the same file first. In practice this makes includes easy because every file can be included relative to that one file. The tradeoff is that you need something like mod_rewrite to get SEO-friendly urls instead of http://example.com/index.php?some_info_to_specify_an_action . Also, your index.php file can be pretty ugly unless you concoct a safe scheme to route code execution to the right file in the right subdirectory. CodeIgniter has a pretty clever one that I like lots.
3) Redefine the include path in your PHP.ini file.
This is only typically feasible if your server hosts only one website or if you can somehow define a php.ini file specific to your website. The idea is that you define the include_path such that it knows where your project path is.
I hope this helps.