PHP require and include files

Including and requiring files with PHP is quite easy. There are a few ways you can do it.

One method is to include a global directory path in your PHP ini file. This will mean the parser will check this directory each time it can't find a file. This does have a slight performance degrading and you may not always have access to the ini file. require '../../../website_files/some.php';

The above code breaks because if a file includes another file that includes another, the path suddenly changes and breaks your code. This is probably one of the worst ways to include files and causes many bugs.

require '/home/www/website_files/some.php';

Including from the root directory is good but will break when you transfer your website onto another server if the new server has a different path. Avoid also.

require HOME_DIR . '/website_files/some.php';

Creating static variables in PHP configuration files is good as it allows you to transfer your site between servers, as long as you change the configuration file. The downside of this approach is that you now have a dependency on the configuration file and must update it for each server.

require dirname(__FILE__) . '/website_files/some.php';

The beginning of this require statement finds the current directory that the calling file resides in, then creates a path from that directory. This has the benefit of having zero dependencies and allowing your code to transfer between sites easily. The downside is that if you move the corresponding file, each calling file will break.

Use whatever method suits you but make sure your code is flexible enough to be transferable between servers.