Home  >  Article  >  Backend Development  >  About the loading process of .env files in the PHP framework

About the loading process of .env files in the PHP framework

藏色散人
藏色散人forward
2020-01-29 13:43:293107browse

About the loading process of .env files in the PHP framework

Many frameworks now use the .env file in the root directory to configure environment variables. PHP itself will not parse this file. You need to use PHP code to read and parse this file and put it in in environment variables.

For example, to view the loading process of the .env file in thinkphp, use the following strace command to view the status of the fpm process stat file.

strace $(pidof 'php-fpm'|sed 's/\([0-9]*\)/-p \1/g') -e stat -s 1024

The returned item is to check whether the .env file exists.

[pid 11692] stat("/data1/mailLog/public/phpdev/xxx/xxx/.env", 0x7fff6ba5f9f0) = -1 ENOENT (No such file or directory)

The code processed is these sentences

if (is_file(ROOT_PATH . '.env')) {
    $env = parse_ini_file(ROOT_PATH . '.env', true);
    foreach ($env as $key => $val) {
        $name = ENV_PREFIX . strtoupper($key);
        if (is_array($val)) {
            foreach ($val as $k => $v) {
                $item = $name . '_' . strtoupper($k);
                putenv("$item=$v");
            }
        } else {
            putenv("$name=$val");
        }
    }
}

For more related php knowledge, please visit php tutorial!

The above is the detailed content of About the loading process of .env files in the PHP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete