Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include vs include_once (speed) [duplicate]

Tags:

php

Possible Duplicate:
Why is require_once so bad to use?

I've read somewhere that the include_once and require_once statements in PHP were slower than their non-once counterparts. Is this a significant slowdown? Has there been any testing or studies of this, and has it changed in recent versions of PHP?

like image 683
Ricket Avatar asked Dec 01 '10 16:12

Ricket


People also ask

What is difference between include () require () and require_once ()?

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.

What is difference between include and include_once in PHP?

include_once ¶ This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns true . As the name suggests, the file will be included just once.

Should I always use require_once?

You should use require_once when you do not need the file's contents included multiple times. It is also helpful if you are working on a script and are unsure if the file has already been included and you do not want it included twice.

What is the difference between include require include_once () Required_once ()?

The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page.


2 Answers

The speed increase is minimal and comes as a reference check is conducted to prevent code duplication. The 'once' appendage is a preventative measure against the same code being executed/included twice..this performing this check comes at a minor speed cost.

If there is ever an instance where you are using _once look into why it is the case, is you code really built in the most efficient way? It is often better to remove the need to rely on _once and produce better code (easier said than done!).

See:

http://forums.digitalpoint.com/showthread.php?t=1693837

http://www.phpbb.com/community/viewtopic.php?f=71&t=565933

http://www.sitepoint.com/forums/showthread.php?t=269085

http://www.quora.com/What-is-the-difference-between-functions-include-and-include_once-in-PHP

like image 122
SW4 Avatar answered Oct 04 '22 19:10

SW4


The include_once and require_once functions are slower than include and require, simply because they keep track of the files that have already been included, to avoid including them more than once.

But that shouldn't matter AT ALL, since there are probably many ways to optimize your application, way more efficient than this one.

like image 22
Nicolas Avatar answered Oct 04 '22 20:10

Nicolas