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.
  1. <html>  
  2. <body>  
  3. <a href="http://www.c-sharpcorner.com/">Home</a>  
  4. <a href="http://www.c-sharpcorner.com/Forums/">Forums</a>  
  5. <a href="http://www.c-sharpcorner.com/Blogs/">Blogs</a>  
  6. <a href="http://www.c-sharpcorner.com/Videos/">Videos</a>   
  7. <a href="http://www.c-sharpcorner.com/Training/">Training</a>   
  8. </body>  
  9. </html>  
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.
  1. <html>  
  2. <body bgcolor="pink">  
  3. <?php include("csharpcorner.php"); ?>  
  4. <h2><b>Welcome C-Sharpcorner Tutorials</b></h2>  
  5. <p>This page created by Vineet Kumar Saini.</p>  
  6. </body>  
  7. </html>  
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.
  1. <html>  
  2. <body bgcolor="pink">  
  3.   
  4. <html>  
  5. <body>  
  6. <a href="http://www.c-sharpcorner.com/">Home</a>  
  7. <a href="http://www.c-sharpcorner.com/Forums/">Forums</a>  
  8. <a href="http://www.c-sharpcorner.com/Blogs/">Blogs</a>  
  9. <a href="http://www.c-sharpcorner.com/Videos/">Vedios</a>   
  10. <a href="http://www.c-sharpcorner.com/Training/">Training</a>   
  11. </body>  
  12. </html>  
  13. <h2><b>Welcome C-Sharpcorner Tutorials</b></h2>  
  14. <p>This page created by Vineet Kumar Saini.</p>  
  15. </body>  
  16. </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
  1. <html>  
  2. <body bgcolor="pink">  
  3. <?php include ("csharp.php"); ?>  
  4. <h3><b>Welcome C-Sharpcorner Tutorials</b></h3>  
  5. <p>This page created by Vineet Kumar Saini.</p>  
  6. </body>  
  7. </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 
  1. <html>  
  2. <body bgcolor="pink">  
  3. <?php require ("csharp.php"); ?>  
  4. <h3><b>Welcome C-Sharpcorner Tutorials</b></h3>  
  5. <p>This page created by Vineet Kumar Saini.</p>  
  6. </body>  
  7. </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.
 
Some Useful Resources