include
The The documentation below also applies to require.
Files are included based on the file path given or, if none is given, the
include_path specified. If the file
isn't found in the include_path,
Note that both
If a path is defined — whether absolute (starting with a drive letter
or For more information on how PHP handles including files and the include path, see the documentation for include_path. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Example #1 Basic
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
Example #2 Including within functions
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags. If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Example #3
Warning
Security warningRemote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code. See also Remote files, fopen and file for related information.
Handling Returns:
Because Example #4 Comparing return value of include
Example #5
If there are functions defined in the included file, they can be used in the main file independent if they are before return or after. If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.
Another way to "include" a PHP file into a variable is to capture the
output by using the Output Control
Functions with
Example #6 Using output buffering to include a PHP file into a string
In order to automatically include files within scripts, see also the auto_prepend_file and auto_append_file configuration options in php.ini.
See also require, require_once, include_once, get_included_files, readfile, virtual, and include_path. |