2018-11-11 Pujan Niroula 1 Minute(s) Read

Difference Between Require_Once and Require in PHP

img-require vs require once.png

How many of you use include or require function during coding in PHP language. Perhaps most of newbie use any of these preloaded functions.

Do you know difference between include and require? If you use include function then even the included file is not found script will run but if you use require function then the script won't run further if the required file is not found. Simply we can say if file is not found include will produce warnings but require will produce fatal error.

Let's go deeper, How many of you know difference between  require() and require_once() or include and include_once?

Key Difference Between require and require_once

Require includes specific file but require_once does that only if it has not been included before. require_once checks file has been included before or not and only include file if file has not been included. 

If you have to include a file on different files several times then you must use require_once.

Some reports shows require is slightly fast than require_once but in real world scenario you won't find difference.

Difference Between Include and Include_once

include() will include the file but include_once() will include file if the file has not been included before.

Exactly same difference as require and require_once but it will show include behavior. This means if the included file is missing then it will just throws warning and keeps running.

Why You Should to use require_once() Instead of require()

Why require_once ?

During the development period of this So Called CMS first I am confused between Require ad Include functions in PHP. I chooses require I had no problem but one day I faced Cannot redeclare xxx() error and realized I placed require function twice for same file.

Almost for all condition you can use require_once instead of require. This will reduce headache for sure...

Difference between require_once() and include_once()

include_once will include file only once if the specific file is not exists then it will throw warning but it keep executing but require_once include file only once if specific file is not found then it throw fatal error and stop executing.

If you need to include a file that is compulsory to execute the script then use require_once but if you want to include a file that is not compulsory to execute whole script then use include_once.

Comment your experience regarding performance and use of these functions.

Comments (13) Add New Comment
Chara2024-07-24 2:00 PM

Thanks for some other excellent post. Where else could anyone get that kind of info in such an ideal manner of writing? I've a presentation next week, and I'm on the search for such information.

GichardantaB2024-06-23 1:17 PM

This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your excellent post. Also, I've shared your website in my social networks! karabash.chelbusiness.ru/index.php?name=account&op=info&uname=unypavaze  cbt-v.com.ua/catalog/all  gmailspva.com/facebook-pva-accounts  museums.artyx.ru/books/c0003_1.shtml  baikal-biz.ru/forum/viewtopic.php?p=168589 

diplomaxose2024-06-14 8:05 AM

?????? ???????? ??????????? ??? ??? ???????? ???????????? (?????) ?? ????????? ?????? ????? ?????? ? ????? ???????. ?????? ??????: ?????????????? ??????? ??? ?????? ??????????? ?? ??????. ????? ?????????, ?? ??????? ??????, ????? ?? ?????? ??????????? ??????? ??????????? ? ????? ???. ? ????? ?????? ????? 100 ?????????? ??????????? ???? ????????? ??????????? ? ???????. ? ??????????? ?? ?????? ??????????? ??????? ??? ??? ??????????? ????????? ? ????? ??? ?? ????? ?????????? ? ?????. - interestbook.ru

TimsothyShubs2024-06-12 12:57 AM

This is my first time pay a visit at here and i am truly happy to read all at alone place. beritanusantara.co.id/cadir-polimdo-okta-lintong-efendy-rasid-maryke-alelo-masuki-tahap-pertarungan-tingkat-kementerian/?  forexrassia.ru/page/4?  ravolutionfestival.com/ravolution202205hcmc/?  woke.party/blogs/104615/%D0%94%D0%B8%D0%BF%D0%BB%D0%BE%D0%BC-%D0%BA%D0%BE%D0%BB%D0%BB%D0%B5%D0%B4%D0%B6%D0%B0-%D0%B2-2024-%D0%B3%D0%BE%D0%B4%D1%83-%D0%9E%D1%81%D0%BE%D0%B1%D0%B5%D0%BD%D0%BD%D0?  usaessayexperts.com/faq/? 

Ismaeleromo2024-06-09 8:59 AM

Hi everyone, it's my first go to see at this web site, and article is genuinely fruitful in favor of me, keep up posting such content. ripet.hu/index.php?  rus-flagman.ru/service.php?  golf.od.ua/forum/viewtopic.php?t=16601&highlight=?  irc71.ru/index.php/jomsocial/groups/viewbulletin/142-za-skolko-imenno-vozmozhno-budet-zakazat-diplom-vuza-sejchas?groupid=47?  ediblehomegardensresort.com/index.php?do=/public/blog/view/id_119635/title_-2024/? 


Hi friends, how is everything, and what you desire to say concerning this article, in my view its actually awesome in support of me.


We stumbled over here by a different web address and thought I might check things out. I like what I see so now i am following you. Look forward to finding out about your web page again.


Wonderful, what a blog it is! This web site gives valuable information to us, keep it up.


If some one wants expert view on the topic of blogging afterward i suggest him/her to visit this web site, Keep up the pleasant job.


Greetings! Very helpful advice within this post! It's the little changes that make the greatest changes. Thanks a lot for sharing!


You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and very broad for me. I'm looking forward for your next post, I will try to get the hang of it!


First off I would like to say wonderful blog! I had a quick question in which I'd like to ask if you do not mind. I was curious to find out how you center yourself and clear your mind before writing. I've had a tough time clearing my mind in getting my ideas out. I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are generally wasted simply just trying to figure out how to begin. Any ideas or tips? Appreciate it!

Pratik2019-10-24 8:30 AM

Thanks, It's what I am looking for... Now I prefer to use require_once() over require().