Web Development

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.

[cc lang=”php”]
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.
[/cc]

include()

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

[cc lang=”php”]
include(“function.php”);
include(“function.php”);
# Output : This code will generate error,
# of function test already declared.
[/cc]

Conclusion

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

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

Shares:
  • Kristopher
    Kristopher
    August 5, 2011 at 1:39 am

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

    Reply
  • danny
    August 5, 2011 at 3:22 am

    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.

    Reply
    • Avinash
      August 5, 2011 at 7:50 am

      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?

      Reply
      • danny
        August 5, 2011 at 7:18 pm

        Okay I gotcha now. Thanks for the confirmation.

        Reply
  • Karoly Bujtor
    Karoly Bujtor
    August 5, 2011 at 1:17 pm

    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.

    Reply
    • Avinash
      August 5, 2011 at 6:36 pm

      Hi Karoly,

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

      Reply
  • SeanJA
    August 5, 2011 at 5:07 pm

    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()

    Reply
    • Avinash
      August 5, 2011 at 11:08 pm

      Yes, you are right about the file name mistype.

      and regarding the second one I have edited the code.

      Thanks

      Reply
  • chris
    August 5, 2011 at 5:50 pm

    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.

    Reply
    • Avinash
      August 5, 2011 at 11:12 pm

      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().

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *