Saturday, January 5, 2013

Include() and Require() Function in PHP


Introduction
In PHP, there are two functions that are used to put the contents of a file containing PHP source code into another PHP file. These function are Include() and Require(). These functions are the same if but they have one difference. The difference is that the include() function produces a warning, but the script will continue execution, while the require() function produces a warning and a fatal error i.e. the script will not continue execution. These two functions are used to put the data of a file into another PHP file before it is executed by the server. Include() Function
The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.
Example
First of all we create a PHP file. Suppose that we created a file called csharpcorner.php, which is later called by another PHP file.

<
body>
<
a href="http://www.c-sharpcorner.com/">Home</a><a href="http://www.c-sharpcorner.com/Forums/">Forums</a><a href="http://www.c-sharpcorner.com/Blogs/">Blogs</a><a href="http://www.c-sharpcorner.com/Videos/">Videos</a> <a href="http://www.c-sharpcorner.com/Training/">Training</a> </body>
The above file save is csharpcorner.php. Now we will create another PHP file. Suppose that we created include.php file. In which we called csharpcorner.php file. We can say that the data of csharpcorner.php file is put into the include.php file using the include() function.
<html>
<
body bgcolor="pink"> include("csharpcorner.php"); ?><h2><b>Welcome C-Sharpcorner Tutorials</b></h2>
<
p>This page created by Vineet Kumar Saini.</p>
</
body>

The above file is saved as include.php.
Output
For executing the script you will write in the address-bar of the browser "http://localhost/FolderName/filename(include.php)".
image1.jpg
When you look at the source code of the above browser then it will look like this. In this code you will see two PHP files combined into one HTML file.
<html>
<
body bgcolor="pink">
<html>
<
body>
<
a href="http://www.c-sharpcorner.com/">Home</a><a href="http://www.c-sharpcorner.com/Forums/">Forums</a><a href="http://www.c-sharpcorner.com/Blogs/">Blogs</a><a href="http://www.c-sharpcorner.com/Videos/">Vedios</a> <a href="http://www.c-sharpcorner.com/Training/">Training</a> </body>
</
html>
<
h2><b>Welcome C-Sharpcorner Tutorials</b></h2>
<
p>This page created by Vineet Kumar Saini.</p>
</
body></html>
Require() Function
The Require() function is also used to put data of one PHP file to another PHP file. If there are any errors then the require() function produces a warning and a fatal error and stops the execution of the script i.e. the script will continue to execute.
Example
<html>
<
body bgcolor="pink"> include ("csharp.php"); ?><h3><b>Welcome C-Sharpcorner Tutorials</b></h3>
<
p>This page created by Vineet Kumar Saini.</p>
</
body></html>
Output
In the above example we used the include() function, in which we called a PHP file (csharp.php) which does not exist. When we execute this script then they will execute without error but produces a warning. You can see in the following image.
image2.jpg
<html>
<
body bgcolor="pink"> require ("csharp.php"); ?><h3><b>Welcome C-Sharpcorner Tutorials</b></h3>
<
p>This page created by Vineet Kumar Saini.</p>
</
body></html>
Output
In the above example we used the require() function, in which we called a PHP file (csharp.php) which does not exist. When we execute this script then they will not execute and produces a warning and fatal error. You can see in the following image.
image3.jpg
Conclusion
So in this article you saw the difference between the include() and require() functions in PHP. Using this article one can easily understand the include() and require() functions in PHP.

No comments:

Post a Comment