requirevsinclude

Include vs Require in PHP – Difference

In my previous article we have seen the basic difference between include() and include_once(). This article will explain how include() and require() are different. require() and include() performing the same function, however the major difference is require() will throw a fatal error (E_COMPILE_ERROR ) since it is not able to find the specific file on specified location and it will stop the execution as a result, on the other hand include() will show the warning (E_WARNING) however it will not stop the script execution. The syntax of both the functions seems like same.

Syntax :

include('filename');
 
or
 
require('filename');

To understand this in detail let’s see one example.

include() :

Suppose we are going to add Helper1.php file using include() and we are not able to find Helper1.php file in that specific location, then It will show a warning and at the same time will continue to execute the below code which has been written after that specific statement.

Index.php

<?php 
include("Helper1.php"); 
echo "<br><br> &nbsp;&nbsp;This is Index File";
?>

Include-warning

In the above image, you can see that it will display two warnings like “Failed to open stream AND Failed to open File” but it is still executing the code which has been written after include() statement. So this way include() will not stop the execution of code.

require() :

If we will use require() in place of include() it will throw the fatal error like below and will stop the execution of code as a result of it.

Index.php

<?php
require("Helper1.php");
echo "<br><br> &nbsp;&nbsp;This is Index File";
?>

Output :

Require vs Include-fatal

In the above image, you can see a warning and a fatal error like ” Failed to open stream AND Failed to opening File” and stop the execution of the script.

When to use include() and When to use require() :

require() will be used when the file is required by your application to continue the execution of the next step, e.g. an important message template file or a file containing configuration variables like database connection variables without which the app flow would break.

include() will be used when the file is not that important to process the next step for the application and which will not break the application during the process. E.g. referencing variables from the current scope file.

Note : It’s wise decision to use include() while adding external file code so that it does not stop the execution of your code, however it depends on the situation’s demand.

In this article, you have found two new words on which you need some more explanation.

  • Warning
  • Fatal Error

To understand this in detail Click here…

 

23 thoughts on “Include vs Require in PHP – Difference

    1. Thank you vikas,

      You can suggest me the topics in which you are not able to find proper information. we are glad to write the best article on that specific topic.

  1. I definitely agree with thid. you did a really good job getting this across.
    Do you use social sites ? I’d like to follow you if that would be ok.
    I’m undoubtedly enjoying your blog and look forward to new posts.
    Thank you for this article

  2. Excellent beat ! I wish to apprentice while you amend your website, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea

  3. Hi my loved one! I wish to say that this article is amazing, great written and come with approximately all significant infos. I would like to peer extra posts like this .

  4. Hi there I am so glad I found your blog, I really found you by mistake, while I was researching on Google for something else, Anyhow I am here now and would just like to say cheers for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the awesome job.

  5. I have to many thanks for the efforts you possess place in writing this website.
    I really hope to discover exactly the same high-grade
    blog articles by you in the foreseeable future also.
    In truth, your creative writing abilities has motivated
    me to acquire my own, personal, personal website now 😉

  6. I’m really experiencing the design and layout of the website.
    It’s a very easy on the eyes rendering it a lot more pleasant to me
    to come here and visit more often. Would you hire out a designer to produce
    your theme? Excellent work!

  7. Terrific work! This is certainly the kind of info that are
    made to be shared around the internet. Shame on the seek engines for not any longer positioning this post upper!
    Happen over and seek advice from my website . Thanks

Leave a Reply