ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What are the differences between require and include, include_once?

The developers use these functions , and I always see them in code , but what is the main differences between them.

user-image
تم إضافة السؤال من قبل Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
تاريخ النشر: 2013/02/19
Mohammad odeh
من قبل Mohammad odeh , Software Engineer , bayt.com

All of these functions are using to include piece of code that located into separate file to another one but the difference between of them as below 1) require If you request to include file that already not found then the "require" function will give you fatal error and the remaining PHP code will not execute.
2) include Again if you request to include file that already not found then the "include" function will give you warning and executes the remaining PHP code.
Difference between require_once and include_once is same as the difference between require and include.
but the "_once" function is using to disallow the PHP code to include the file multiple times.
so if you need to include the file more the one time use require or include for that.

Charles Offodile
من قبل Charles Offodile , Sedco Forex , Noble Drilling co Ltd ,Dual Drilling co Ltd, Schlumberger international, Petro-smart completions Ltd and Total E&P Usan Fpso

Which could represent the needed and added in turn means what is needed( a must ) to do the job and what should be added as for back up.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

just go to dictionary, don't waste time

AUGUSTINE ASAKIZI   NJI
من قبل AUGUSTINE ASAKIZI NJI , Senoir lecturer , National Polytechnic Bamenda

REQUIRE IS SOMETHING NECESSARILY NEEDED FOR A PROCESS WHILE INCLUDE IS JUST AN ADDITIONAL ASPECT TO A SSYSTEM

Afsheen Atif
من قبل Afsheen Atif

The include function is used in PHP when you want to include a file within the current process.This can be used in a php templating system where you have a header at the top of the page and the header is going to be the same on all pages. You will put this code inside it's own file and use the include function to add the file to the page.

If the file that you want to include is not found then this function to return a PHP warning but will continue with the application.

 

The include_once function is exactly the same as the include function except it will limit the file to be used once.

A practical use of this function is if you define any functions in the included file to avoid redefinition of a function you should include it with a include_once.

 

The require function acts just like the include function except if the file can not be found it will throw a PHP Fatal error E_COMPILE_ERROR which will stop the application continuing,. As the name suggests this file is required for the application to work correctly.

You can read here in depth.

Cheers!

Rani Alomari
من قبل Rani Alomari , Full Stack .Net Developer , Jordan Center for Diseases Control (JCDC)


1- require: if the file not exist then HTTP /500 will be retured to response.
1-require_once: the same as require but if the require executed more than once then it will be included only one time.
for example if you require function x two times then using require_once will include one copy of the function x.
1.
include: if the file not exist then the request will be resumed without returning any error.

المزيد من الأسئلة المماثلة