PHP include() and require() Function


- Advertisement -

Content from one PHP file can be included into another PHP file before it gets executed at the server. in order to perform this operation, PHP provides two functions namely include() and require(). This is a way in which PHP allows the reusability of code. Moreover, elements like headers, menus, footers, and functions can be directly imported from one file to another file and thus, they can be used on multiple pages.

The include() Function

This function simply takes the text in the specified file and uses it for the file concerned as and when it is required. There may be issues in loading a file as a result, only a warning for the same is generated. However, this will not stop the file from getting executed.

A good example of a scenario where you might require file inclusion is when you may want to create a common menu for all your webpages. The contents of the file, which can be named as menu.php, given below, can be referred for code.


The menu created using this file will have links for three websites namely PHPGurukul, Google, Facebook. Moreover, the text corresponding to the same will be displayed. in order to include this file, a code similar to the one given below must be written.

The require() Function

This function basically copies text from the specified file and placed it into the target file. Therefore, it is as good as writing the code without the programmer having to put in the effort for it. However, if there is a problem in loading the file, the program’s execution halts and generates a fatal error.


In entirely, the include() and require functions are the same with the only diffrence in the way they handle tehir error conditions. With that said, the use of require() is recommended as against include() because the execution of a script in the absence of missing files can lead to unpredicatable behaviour.

Next tutorials: PHP include_once() and require_once() Function

- Advertisement -

Leave A Reply

Your email address will not be published.