PHP – File Inclusion,You can include the content material of a PHP document into any other PHP document before the server executes it. There are PHP functions which can be used to covered one PHP record into every other PHP record.
- The consist of() Function
- The require() Function
This is a sturdy point of PHP which enables in developing features, headers, footers, or elements that may be reused on multiple pages. This will help developers to make it smooth to alternate the layout of complete website with minimum attempt. If there’s any alternate required then as opposed to changing thousand of documents just alternate included document.
PHP – File Inclusion,The include() Function
The include() feature takes all the textual content in a specific record and copies it into the record that uses the encompass feature. If there may be any hassle in loading a document then the encompass() function generates a caution however the script will keep execution.
Assume you need to create a not unusual menu for your internet site. Then create a file menu.Personal home page with the following content material.
<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> <?php include("menu.php"); ?> <p>This is an example to show how to include PHP file!</p> </body> </html>
It will produce the following result −
The require() Function
The require() function takes all of the textual content in a unique record and copies it into the record that makes use of the encompass function. If there is any hassle in loading a file then the require() characteristic generates a deadly blunders and halt the execution of the script.
So there’s no difference in require() and consist of() besides they take care of blunders conditions. It is recommended to use the require() feature in place of encompass(), due to the fact scripts have to no longer preserve executing if files are lacking or misnamed.
You can strive the usage of above instance with require() characteristic and it’s going to generate identical end result. But if you may attempt following examples where report does now not exist then you’ll get distinct results.
<html> <body> <?php include("xxmenu.php"); ?> <p>This is an example to show how to include wrong PHP file!</p> </body> </html>
This will produce the following result −
This is an example to show how to include wrong PHP file!
Now lets try same example with require() function.
<html> <body> <?php require("xxmenu.php"); ?> <p>This is an example to show how to include wrong PHP file!</p> </body> </html>
This time file execution halts and nothing is displayed.
NOTE − You may additionally get undeniable caution messages or deadly errors messages or nothing in any respect. This depends for your PHP Server configuration.