Current Location: Home> Latest Articles> What is the default return value of get_include_path()? How to adjust?

What is the default return value of get_include_path()? How to adjust?

gitbox 2025-05-17

The get_include_path() function returns the current included path of PHP. These paths are the default locations when PHP looks for files, and are usually used for include , require and other file containing statements.

By default, the path returned by get_include_path() usually contains the following locations:

  • The directory of the current script.

  • include_path set in the PHP configuration file ( php.ini ).

  • The default PHP installation directory.

For example, in a standard PHP environment, when calling get_include_path() , the default path returned is usually as follows:

 .:/usr/local/lib/php

This means that PHP will first look for the file ( . ) in the directory where the current script is located, and then look for it in /usr/local/lib/php .

If not specified, the default include path is usually closely related to the server configuration and will depend on settings in the php.ini configuration file.

2. How to modify and adjust the return value of the get_include_path() function?

The get_include_path() function is only used to get the current include path. If you want to modify or set the include path of PHP, you can use the set_include_path() function. set_include_path() allows you to modify the directory where PHP looks for files and set a new include path.

2.1 Use set_include_path() to modify the included path

You can adjust the include path in the current PHP environment through the set_include_path() function. This function takes a string as an argument, and the string contains a new containing path. Paths can be multiple paths separated by colons (in Linux/Unix systems) or semicolons (in Windows systems).

 // Get the current include path
$current_path = get_include_path();
echo "Currently included path:$current_path";

// Modify the include path
$new_path = "/path/to/your/libs:/another/path/to/libs";
set_include_path($new_path);

// Get the modified include path
$updated_path = get_include_path();
echo "Modified include path:$updated_path";

In this example, we set the include path to /path/to/your/libs and /another/path/to/libs and confirm the modified path through the get_include_path() function.

2.2 Set include_path in php.ini file

In addition to modifying the included path in the code through the set_include_path() function, you can also change the default include_path by editing the PHP php.ini configuration file. In php.ini , you can set the include_path option, for example:

 include_path = ".:/usr/local/lib/php:/path/to/your/libs"

After modifying the php.ini file, you need to restart the PHP service to make the changes take effect.

2.3 Dynamic adjustment includes paths

If you need to dynamically adjust the include path according to different environments or configurations, you can use the set_include_path() function in combination with environment variables. For example, in some cases, you may want to dynamically adjust the include path based on user input or a specific configuration file:

 if (getenv('USE_CUSTOM_LIBS') == 'true') {
    set_include_path('/custom/libs:' . get_include_path());
}

In this example, we checked an environment variable USE_CUSTOM_LIBS , and if the value of that variable is true , dynamically modify the include path.