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

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: