include() vs include_once()

There is a big difference in these functions. Actually both functions perform the same task which includes the file but the process of including the file a quite different.

include_once()

This function include the file and make sure that same file will not get included again.

For example if your file say function.php have one function test.

include("function.php"); include_once("function.php"); # Output : This code will not generate any error, # Because include_once will make sure that # function.php to include only once.
include("function.php");
include_once("function.php");

# Output : This code will not generate any error,
# Because include_once will make sure that
# function.php to include only once.

include()

This function also include the file regardless of checking the inclusion of the file in previous code.

include("function.php"); include("function.php"); # Output : This code will generate error, # of function test already declared.
include("function.php");
include("function.php");
# Output : This code will generate error,
# of function test already declared.

Conclusion

If you know what you are doing then its better to use include.

Note: include_once() will check for whole code to check the existance of the file, So its better to use include() over include_once().

Share This Article

  • Kristopher

    Will put this into practice. A++++…

  • http://mulvid.com danny

    It seems like the argument you’re making is that include_once() would be better, but then your conclusion says to use include(). Can you explain this.

    • http://www.xpertdeveloper.com Avinash

      The think I want to say is, include_once() will be overhead for the code to check the file existence is the current code. So include() will be better to use. Actually I have explain the flow of include_once() that how it will include the file. Now any confusion?

      • http://mulvid.com danny

        Okay I gotcha now. Thanks for the confirmation.

  • Karoly Bujtor

    Well, I think the first example is wrong because you use firstly include_once and than include. If you change the order it will be OK.

    • http://www.xpertdeveloper.com Avinash

      Hi Karoly,

      Thanks for pointing this, I have made changes accordingly..

  • http://seanja.com SeanJA

    You are micro optimizing again….

    Also, both of them will fail you if you mistype the file name. They will only throw a warning which you may or may not have enabled on your server.

    Also, the first code will fail the same as the second code:

    Fatal error: Cannot redeclare test()

    • http://www.xpertdeveloper.com Avinash

      Yes, you are right about the file name mistype.

      and regarding the second one I have edited the code.

      Thanks

  • http://phpdeveloper.org chris

    You forget one thing, though – include_once has the added benefit of only including something one time. If you don’t use it you’ll get “cannot redefine” errors thrown if your application tries to include the same file again. This is particularly important in frameworks and the like where dynamic filenames/classes could be loaded constantly.

    • http://www.xpertdeveloper.com Avinash

      Hi Chris,

      First of all i am not in excuse mode.

      but yes it could be a benefits in some cases. But if you are the main developer of any framework and you know what is included there in the code then its fine to use the include().