Current Location: Home> Latest Articles> Use get_include_path() to get the actual application of the current script containing the path

Use get_include_path() to get the actual application of the current script containing the path

gitbox 2025-05-17

In PHP, the include path is a collection of paths used to tell the PHP engine where to load files. When you use include or require statements in your code, PHP will search for files based on these paths. By default, PHP adds the directory where the current script is located and the path specified in the PHP configuration file to the include path.

2. Use of the get_include_path() function

The get_include_path() function returns the current include path. The basic syntax is as follows:

 $include_path = get_include_path();

This function returns a string containing multiple paths separated by the system's path separator (colon ":" in Linux and macOS; semicolon ";" in Windows).

3. Practical application

In actual development, the get_include_path() function can help you understand the include paths of the current PHP environment, especially when you need to dynamically modify or adjust the include paths. Through the set_include_path() function, developers can modify the included path, which in turn affects the behavior of file loading.

Example 1: Get the currently included path

 <?php
// Get the current include path
$current_include_path = get_include_path();
echo 'Currently included path:' . $current_include_path;
?>

The output may be similar to:

 Currently included path:.:/usr/local/lib/php:/var/www/html

This example shows the inclusion path of the current script, where . represents the directory of the current script.

Example 2: Modify the include path

Sometimes, you may need to modify the include path of PHP at runtime. For example, you can add specific directories to include paths so that PHP will look for files in these directories.

 <?php
// 获取Currently included path
$include_path = get_include_path();

// New directory path
$new_include_path = '/path/to/my/includes';

// Modify the include path
set_include_path($include_path . PATH_SEPARATOR . $new_include_path);

// Verify the modified include path
echo 'Modified include path:' . get_include_path();
?>

In this example, we add the new path /path/to/my/includes to the existing include path through the set_include_path() function.

Example 3: Dynamically set the include path and include files

Sometimes you may set different inclusion paths according to different conditions. For example, load different library files according to the current environment:

 <?php
// Include paths according to environment settings
if ($_SERVER['SERVER_NAME'] === 'production.gitbox.net') {
    set_include_path('/path/to/production/includes');
} else {
    set_include_path('/path/to/development/includes');
}

// Files can now be included in the new include path
include 'myLibrary.php';
?>

This example shows how to dynamically modify the include path according to different server environments and load the appropriate library files.

Example 4: Loading remote files via URL

Sometimes we need to load remote files, such as loading PHP files from a URL. PHP does not support loading files directly through URLs by default, but this behavior can be allowed using the allow_url_include configuration option.

 <?php
// Assume that allows URL load
$include_path = get_include_path();

// use URL load远程文件
include 'http://gitbox.net/remote/path/to/file.php';
?>

In this example, we load the remote PHP file through the URL to ensure that the include path is configured correctly, and define the allow_url_include setting in the file.