Articles in Basics Category

PHP Include Files

PHP lets you add content of another file into a running PHP file. It’s basically like merging two files at run time. This let you have common functions and content in one file and use it as necessary.

For an example, think that you are writing a program on employees in a company. Many PHP files in your program would need to get employee full name. Rather than writing a function for it in each file, you can write the function in a separate file (say common-functions.php) and include it wherever needed.

function getEmployeeFullName($employeeId) {
	// Write code to return full name based on $employeeId

You can include common-functions.php in another file as below.


echo 'Name of first employee is ' . getEmployeeFullName(1);

You can include any number of files to another file. But including comes with a little performance cost. Therefore include only the files which are really required.


include_once() is similar to include() but makes sure that the mentioned file is included only once during the runtime of current file. Once a file is included, its functions and variables are available to the current file and you may reassign its variables with different values.

If you included external file more than once with include(), there is a chance that the values of the variables you changed would be reassigned. include_once() can prevent this happening. If you called include_once() more than once giving it same file path, it would only include the file once. But include() may include the file each time you call it.

include() vs require()

require() behaves as same as include(). Only difference is, if require() couldn’t find the mentioned path, it would issue a fatal error and aborts executing the rest of current file whereas include() would only issue a warning and executes rest of the file.

include_once() vs require_once()

Again require_once() is identical to include_once() and the difference is it issues a fatal error when the file is not found whereas include_once() only issues a warning.

Include Path

If the include file is in the same server as the current file, path can be specified relatively. If both files are in same folder, you can include the file as below.


If common-functions.php is at a folder one above the folder of current file, you can call it as below.


If the include file is in a remote server, and URL fopen wrappers are enabled in your server’s php.ini file (which has been enabled by default from version 4.0.3), you can call the remote file via its URL as below.


Note that if remote server is able to process PHP files, you would only get the output of remote PHP file, not the file contents.

Extension of Include File

Extension of external file doesn’t have to be .php. It can also be in other extensions. One popular such extension is .inc. As long as PHP interpreter can read the content of the file (content should be text) it should be ok.

When including remote files, if the extension of the file hasn’t been set as an extension for PHP files in remote server’s configuration files, that file won’t be interpreted in remote server even though it may contain PHP code. So, you would get the content of the file instead of its output.

/* Would include file contents if .inc hasn't been set
to be interpreted by PHP interpreter */

Braces Are Optional

All four include statements are language constructs and using braces with them is optional. So, you can include common-functions.php as below without using braces.

include 'common-functions.php';
Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.