Major difference between include and require

You people know very well there are 4 ways to include a file in PHP.

1. include().

2. include_once();

3. require().

4. require_once().

Because of four existence, sometimes beginners gets confuse which right one to use in their code. So in this post, i am going to give you a simple explanation of these four.

include() – This function is used to include a file in the current page and it take single argument. You can specify the file path either relative or absolute path. If the specified path is not found, then it will throw Warning message but still the application continues to run…This is good for production environment.

include_once() – This is similar to include() but only difference is it will include the file only once in the page. For example, you have an loop which contains common code to use in different places, and you put that common code in a separate file. So in this case include_once() will include the code only once even its running in the loop.

require() – This function will include a file in the current page and it take single argument. And this is also similar to include but it will throw fatal error if the specified file path is not found and the application will stops running further.

require_once() – This function will do action of require() and include_once(). So it will include only once and throw fatal error if the file is not found.

In common, there is a big difference between include and require, the include will run the file directly whereas require it will check the entire code in the file and then only it will execute. So require() will be slow when compare to include(). And most developers, will recommend to use require, because it will throw error even if it is there simple or small mistake in your code.

Hope i have explained to you people somewhat clearly….


Author: Vinodkumar Saravana



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: