Loading Symbol

How to Include PHP Files – Both Windows and Linux Compatible

PHP Code

This article will show you how to include a PHP file so that it works on both Windows and Linux environments. Whether you’re using WordPress, another PHP framework, or no framework at all, there are many ways to include PHP files. If you’re developing in a Windows environment (e.g. WAMP Server) and deploying your code to a Linux server environment, you need to take care that your include statements are cross-platform compatible. Here is an example:

Include Code

include_once(dirname(__FILE__).'/../my_include.php');

Loading GIF

How It Works

The __FILE__ constant is one of the Magic Constants that PHP makes available to any script. Its value is the absolute path including the file name of the current file. That is wrapped in the dirname function which returns the absolute path of the specified file. Finally, we add the location of the target include file relative to the current file. We have to start with “/” because dirname doesn’t return the path with an ending slash. “../” means go up one directory. Then we have the name of our include file. The whole thing is wrapped in an include_once function to perform the include of the file we specified.


Loading Symbol


Leave a Reply

Your email address will not be published. Required fields are marked *