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> This is Index File";
?>
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> This is Index File";
?>
Output :
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…
This is the best article that I ever found which is explained all the things in detail.
Thank you so much joy.
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.
This is really a good article and very easy to understand.
Thank you.
Awesome
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
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
Hello Malia,
You need to subscribe via email id for more blogs post notifications. We have provided the “SUBSCRIBE VIA EMAIL” feature for the same.
Thank you so much for your valuable feedback.
I value the info on your web site. thnx!
Thanks with regard to supplying like terrific data
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 .
Great website! I am loving it!! Will come back again. I am taking your feeds also
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.
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 😉
It’s great that you will be getting ideas from this post as well as from our argument made at this particular place.
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!
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
bookmarked!!, I like your blog!
Great website! It looks really expert! Maintain the good work! Also, I’ve shared your website in my social networks.
Amazing things here.
Thank you
I appreciate you finally writing about >Require vs Include in PHP – Difference – READMYVIEWS.COM Loved it!
Thanks for sharing your thoughts about other. Regards
Thanks a lot! This is an awesome web-site!.
You have got fantastic thing these