PHP File Inclusion include() and require() functions
PHP File Inclusion include() and require() functions
You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP
functions which can be used to included one PHP file into another PHP file.
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused
on multiple pages. This will help developers to make it easy to change the layout of complete website with
minimal effort. If there is any change required then instead of changing thousand of files just change included file.
Assume you want to create a common menu for your website. Then create a file menu.php with the following
content.
<a href="http://www.tutorialspoint.com/index.htm">Home</a> -
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a> -
<a href="http://www.tutorialspoint.com/ajax">AJAX</a> -
<a href="http://www.tutorialspoint.com/perl">PERL</a> <br />
Now create as many pages as you like and include this file to create header. For example now your test.php file
can have following content.
<html>
<body>
</body>
</html>
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 1/2
4/27/2018 PHP File Inclusion include() and require() functions
So there is no difference in require and include except they handle error conditions. It is recommended to use the
require function instead of include, because scripts should not continue executing if files are missing or
misnamed.
You can try using above example with require function and it will generate same result. But if you will try
following two examples where file does not exist then you will get different results.
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP
Server configuration.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 2/2