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.

Tags:php,
Comments (8) Add New Comment

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

Reply

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.

Reply

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

Reply

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.

Reply

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

Reply

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!

Reply

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!

Reply
Pratik2019-10-24 8:30 AM

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

Reply