include_once() is similar to include() but makes sure that the mentioned file is included only once during the runtime of current file. Once a file is included, its functions and variables are available to the current file and you may reassign its variables with different values.
If you included external file more than once with include(), there is a chance that the values of the variables you changed would be reassigned. include_once() can prevent this happening. If you called include_once() more than once giving it same file path, it would only include the file once. But include() may include the file each time you call it.
include() vs require()
require() behaves as same as include(). Only difference is, if require() couldn’t find the mentioned path, it would issue a fatal error and aborts executing the rest of current file whereas include() would only issue a warning and executes rest of the file.
include_once() vs require_once()
Again require_once() is identical to include_once() and the difference is it issues a fatal error when the file is not found whereas include_once() only issues a warning.
If the include file is in the same server as the current file, path can be specified relatively. If both files are in same folder, you can include the file as below.
If common-functions.php is at a folder one above the folder of current file, you can call it as below.
If the include file is in a remote server, and URL fopen wrappers are enabled in your server’s php.ini file (which has been enabled by default from version 4.0.3), you can call the remote file via its URL as below.
Note that if remote server is able to process PHP files, you would only get the output of remote PHP file, not the file contents.
Extension of Include File
Extension of external file doesn’t have to be .php. It can also be in other extensions. One popular such extension is .inc. As long as PHP interpreter can read the content of the file (content should be text) it should be ok.
When including remote files, if the extension of the file hasn’t been set as an extension for PHP files in remote server’s configuration files, that file won’t be interpreted in remote server even though it may contain PHP code. So, you would get the content of the file instead of its output.
/* Would include file contents if .inc hasn't been set to be interpreted by PHP interpreter */ include('http://www.example.com/common-functions.inc');