Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between require and include function in php?

user-image
Question added by Mohammad Irfan , Certified Magento Developer , Xcite.com
Date Posted: 2013/10/08
Ameen Sarsour
by Ameen Sarsour , Senior Software Engineer , OpenSooq السوق المفتوح

Hi Mohammad

 from php.net

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

 

The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

George Dimitrov
by George Dimitrov , Unix System Administrator , ADVANCED.IO

require

when the file is required by your application, e.g. an important message template or a file containing configuration variables which with without the app would break.

 

require_once

when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

 

include when the file is not required and application flow should continue when not found, e.g.

great for templates referencing variables from the current scope or something

 

include_once

optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

 

 

Using require or include implies that your code is not reusable elsewhere, i.e. that the act of using require or include is actually executing code instead of making available a class or some function libraries. If you are require/including code that does that, that's procedural code, and you need to get to know a new paradigm. I suggest functional programming and object oriented programming.

Muhammad Majid Saleem
by Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

Difference in simple words:

INCLUDE

generates a WARNING if to be inlcuded file doesn't find with CONTINUING application execution

 

REQUIRE

generates a FATAL ERROR if to be included file doesn't find and STOPs application execution

Deleted user
by Deleted user

The key difference between require() and include() is that if you require() a file that can't be loaded (eg if it isn't there) then it generates a fatal error which will halt the execution of the page completely, and no more output will be generated. On the other hand, if you include() a file that can't be loaded, then this will merely generate a warning and continue building the page.

عصام سليمان محمد أحمد المنشاوي
by عصام سليمان محمد أحمد المنشاوي , فني هاردوير , شركة البراق للصناعات الالكترونية

The include function is used in PHP when you want to include a file within the current process. It takes one argument which will be a string to the file path you want to include.

 

include 'main_page.php

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.

include 'header.php'; <div id="content"> </div> include 'footer.php

 

 

Deleted user
by Deleted user

if a .php file is 'required' the following code of PHP won't be get executed if there is any error including the 'require' file.

 

But in case of include, You can continue 'with a warning' even if the file inclusion causes any errors..

 

If you are using 'include' or 'require' same library from many files you are including, its better to use 'include_once' and 'require_once'

Lokesh Gajbhiye
by Lokesh Gajbhiye , Software Programmer , Datacomp Web Technologies (India) Pvt. Ltd

In more simple phrase -

required is a kind of validator which in turns to be executed successfully with no bugs or alias.while include is an extension to your existing program which is used to add additional features may or may not work successfully but won't disturb your other sections of your application. required generates an fatal error.while include echoes only warning to an end user.

Rushik Kansara
by Rushik Kansara , Web Developer , Oceans Technologies

require will produce a fatal error (E_COMPILE_ERROR) and stop the script

include will only produce a warning (E_WARNING) and the script will continue

Fadi Alkhateeb
by Fadi Alkhateeb , Senior Front End Developer , NexTwo

Include and Require are identical, they used to include script in the page.

The difference is when fail:

Require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

Include will only produce a warning (E_WARNING) and the script will continue.

Muhammad Abid Saleem
by Muhammad Abid Saleem , Software Engineer , PVT Company LTD lahore

Difference between include() and require

 

The difference between include() and require is simply, when the specified file is called in both cases , and the file does not exist for example, the require() function returns a fatal error while the include() function, returns a warning, which means the scripts calling the function does not halt, as in the case of with the require().

Short explainatoin for best understanding

 

Its basically, the include() function in a script would show a warning if the specified file does not exist and the require(), function would return an error and halt if the file been called for evaluation or execution does not exist for example. The require makes the file inclusion compulsory , while the include can continue with the other part of the calling script if applicable, if that specified file in the function does not exist.