In order to include the content of another PHP page in the current PHP page we can use two methods provided by PHP which are:
- include()
- require()
With the above two methods we can make HEADER, FOOTER, FUNCTIONS when user is working on another page and then they can able to include all those methods which the user have included in the previous page. This will help a lot in creating website with minimal effort and if there changes need to be made we can do so easily by just changing the code of that include file.
The only difference between include() and require() is that -
include() will display all the data of the included file into the file that called include() function, but if the file does not exist it will show a warning but will continue executing the script.
On the other hand require() will display all the data of the included file into the file that called require() function, but if the file does not exist it will show a error and will halt the execution of the script.
For example : if we try to include a header.php file which does not exist then the output of the two methods will be:
<html>
<body>
<?php include("header.php"); ?>
<p>Text will be displayed even if file does not exist</p>
</body>
</html>
The output will be:
Text will be displayed even if file does not exist
<html>
<body>
<?php require("header.php"); ?>
<p>No output will be displayed if the file does not exist</p>
</body>
</html>
No OUTPUT in this case.
0 Comment(s)