php include
How to include or require other files in a PHP script
Notes
The include and require functions are used to include other source files in the current script. These functions are identical except on failure:
require
will throw a fatalE_COMPILE_ERROR
include
will emit a warningE_WARNING
Cascading Includes
Inclusions can be cascaded. In this example, a variable and constant is dinfed in a file indirectly included in the example: See example1
$ php example1/example.php
Running: example.php
CALLED: A $adjective $color $fruit
RESULT: A
DEFAULT_FRUIT: DEFAULT_FRUIT
Now including f_inner.php
Included: f_inner.php
f_inner defining : $color and $fruit
Included: f_inner_inner.php
f_inner_inner.php defining $adjective and DEFAULT_FRUIT
CALLED: A $adjective $color $fruit
RESULT: A juicy green pear
DEFAULT_FRUIT: apple
Include Paths
Unless absolute paths are specified for the
When evaluating a include/require function call, PHP will look in these locations:
- the absolute file path given
- under theinclude path
- calling script’s own directory
- current working directory
- else fail
Credits and References
- include - PHP function reference
- get_include_path - PHP function reference
- require - PHP function reference
- include path