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()
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.