Difference Between Include() and require() Function

In this article we are looking at the difference between include() and require() function one of the most important functions in PHP. include() and require(). in this article, we are discussing where to use include and require function in your PHP program.  there is a situation where you wanted to know where to use include function and where to use require function in that situation you need to first understand what is the difference between include and require function.  

So read this article and I will give you the complete understanding of how you can use these different methods in your program with their requirement. so this method is not very difficult to understand but it is still very important to know about the that each of these functions does the same work. These two functions are equivalent to each other but there is still something that you need to know when you’re using this function.

we wanted to discuss the include function first.


Include() Function

Include function mostly used to store HTML5 and PHP program in one file and reference this program or you can say reference this code to another web page file. let me tell you the same sentence in another way. for example. if you wanted to create the header for your web page then you can directly insert it in your index file but there is still one other method to separate your code and maintain it properly. create the header.php file and insert all the header code in this file. then what you need to do is just reference this file in the head section of the index file using include function. this may cause to include the code in the header section from the header.php File. 

Using include function you can create HTML and PHP  program in the separate file and put this file as a reference in your main file.

take a look at the simple example.



So in the above example, you will see we created two files.  what is Sample.php and second one is header.php. in the sample.php file we did not add any PHP header code but still it exists. so how does it happen?  because we added include function in the header tag. if you look at the code you can find include function in the header section of the page.  that function is basically a parameter which refers to the filename which you wanted to include.

So here we just specify header.php.  now you wanted to run only sample.php file because this file has include function in the header section.  so when you run this the web server will call include function and include the reference file in the sample.php file.

To know the difference between include() and require() function exactly. Now take a look at the require() function.


require() function

So require() function exactly does the same job like include function but there’s still something very interesting and very important disparity when you are using require() function.  now let me tell you very interesting difference between include and require function.

The PHP server unable to find the file you difference It will produce a warning, But the PHP server continues to process the rest of the program.  this may be harmful to your entire project. There may be a time where to don’t want to execute your program and don’t want to continue Your process if the crucial Include file is missing.  instead, you want to stop your program and immediately produced an error message to the user. This is where the required function come in hand.

Require() function does the same job like include function except for one difference it forces the PHP server to stop processing the program and throw an error message to the user if the reference file is invalid or fail to load.

look at the example.



In the sample.php file, we just change the PHP code and replace include() function to require() parameter is exactly same as include() function.

In the header file, you can put any kind of program you want.  just like if you wanted to create navigation the header section or any kind of go that you are sitting in the main header file will be included in the external PHP file.

so that’s it.

I hope you understand the difference between include() and require() function. to understand how to include and require function work read relevant article on this website will help you to understand more about this function.

So that is all for this article.  we will take you back again in the next article with the new topic.