Current Location: Home> Latest Articles> get_include_path() and get_cfg_var() to obtain the included path in the PHP configuration

get_include_path() and get_cfg_var() to obtain the included path in the PHP configuration

gitbox 2025-05-26

In PHP development, including path is a very important configuration item, which determines the directory order in which PHP searches files when using include , require and other statements. Rational management of included paths can not only improve the maintainability of the code, but also avoid file reference errors. This article will introduce how to use the get_include_path() function and get_cfg_var() function to obtain and manage the included paths in PHP configuration, and demonstrate specific application methods.

1. Include path introduction

The included path of PHP is a string, usually composed of multiple paths separated by semicolons (under Windows) or colons (under Unix/Linux). By default, the included path of PHP may contain the current directory ( . ), system directory, or custom directories. By adjusting the included path, programmers can manage the loading of code files more flexibly.

2. The function of get_include_path()

get_include_path() is a PHP built-in function that gets the include path for the current script runtime. It returns a string with the same format as the include_path value in the configuration file.

Example:

 <?php
echo get_include_path();
?>

After execution, the currently containing the path string will be output.

3. The function of get_cfg_var()

The get_cfg_var() function is used to get the original value of the configuration item in the PHP configuration file (php.ini). It is similar to ini_get() , but get_cfg_var() only returns the values ​​in php.ini and is not affected by runtime modifications.

For example, to get the include_path configuration in php.ini, you can write it like this:

 <?php
$path = get_cfg_var('include_path');
echo $path;
?>

4. Use two functions to obtain and manage the included paths

You can know the default include path in the php.ini file through get_cfg_var('include_path') , and get_include_path() reflects the include path in the current running environment (may have been dynamically modified by functions such as set_include_path() ). Combining these two can help developers better understand and manage changes in included paths.

For example:

 <?php
// Getphp.iniDefault ininclude_path
$defaultPath = get_cfg_var('include_path');

// Get当前脚本运行时的include_path
$currentPath = get_include_path();

echo "PHP配置文件Default ininclude_path:\n";
echo $defaultPath . "\n\n";

echo "The current operating environmentinclude_path:\n";
echo $currentPath . "\n";
?>

5. Example: Dynamically add paths and manage them

Suppose we want to add a new include path /var/www/gitbox.net/includes on the original basis and keep the other paths unchanged:

 <?php
// Get当前包含路径
$currentPath = get_include_path();

// New paths to add
$newPath = '/var/www/gitbox.net/includes';

// Determine whether the new path already exists
if (strpos($currentPath, $newPath) === false) {
    // If not, add,Delimiter automatically sets according to the system
    $separator = PATH_SEPARATOR;  // Windowsyes; Unix/Linuxyes:
    $newIncludePath = $currentPath . $separator . $newPath;

    // Set a new include path
    set_include_path($newIncludePath);

    echo "The new include path has been set:\n";
    echo get_include_path();
} else {
    echo "Include path already contains the specified directory,No need to add repeatedly。\n";
}
?>

6. Summary

  • get_cfg_var('include_path') is used to read the original configuration in php.ini, suitable for debugging and understanding the default configuration.

  • get_include_path() is used to get the current runtime include path, which may contain runtime dynamic adjustments.

  • Using the two together can clearly distinguish between default configuration and runtime status.

  • Use set_include_path() to dynamically adjust the included path, thereby flexibly controlling file search strategies.

Through these methods, PHP developers can better grasp and manage the inclusion paths of programs, and improve the flexibility and robustness of their code.