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

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

藏色散人
Release: 2023-04-08 11:18:01
forward
3139 people have browsed it

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)
Copy after login

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");
        }
    }
}
Copy after login

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!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template