Home > Backend Development > PHP Tutorial > laravel-php putenv function cannot be used in Chinese

laravel-php putenv function cannot be used in Chinese

WBOY
Release: 2016-10-11 14:23:25
Original
1352 people have browsed it

If you know laravel, you can take a look at the above section; if you don’t, just go straight below the dividing line;

Any item in .env, here is MAIL_DRIVER as an example
Change it to "my" and use the env function, and you can get the value normally;
Change it to "I" and use the env function, and you can't get it normally;

<code>MAIL_DRIVER=我
var_dump(env(MAIL_DRIVER''));  // NULL


MAIL_DRIVER=我的
var_dump(env(MAIL_DRIVER''));  // 我的</code>
Copy after login
Copy after login

This situation will also occur with other Chinese words such as "good";
I have to say; laravel is broad and profound; I have studied the source code of the env function;
The problem I finally identified is;
--------- ----------------------------------------Understand and understand the dividing line between laravel framework- --------------------------------------------------

<code>putenv("PROJECT_NAME=我的");
phpinfo();</code>
Copy after login
Copy after login

You can search for PROJECT_NAME in Environment in phpinfo

<code>putenv("PROJECT_NAME=我");
phpinfo();</code>
Copy after login
Copy after login

Project_NAME cannot be found in Environment in phpinfo

Dear friends, why is this? How to set Chinese value without any problem?

Reply content:

If you know laravel, you can take a look at the above section; if you don’t, just go straight below the dividing line;

Any item in .env, here is MAIL_DRIVER as an example
Change it to "my" and use the env function, and you can get the value normally;
Change it to "I" and use the env function, and you can't get it normally;

<code>MAIL_DRIVER=我
var_dump(env(MAIL_DRIVER''));  // NULL


MAIL_DRIVER=我的
var_dump(env(MAIL_DRIVER''));  // 我的</code>
Copy after login
Copy after login

This situation will also occur with other Chinese words such as "good";
I have to say; laravel is broad and profound; I have studied the source code of the env function;
The problem I finally identified is;
--------- ----------------------------------------Understand and understand the dividing line between laravel framework- --------------------------------------------------

<code>putenv("PROJECT_NAME=我的");
phpinfo();</code>
Copy after login
Copy after login

You can search for PROJECT_NAME in Environment in phpinfo

<code>putenv("PROJECT_NAME=我");
phpinfo();</code>
Copy after login
Copy after login

Project_NAME cannot be found in Environment in phpinfo

Dear friends, why is this? How to set Chinese value without any problem?

I also tried it and found that as long as I call putenv('PROJECT_NAME=off') (four bytes, the same for Japanese kana) in the cgi environment, it will fail and return false, while in the cli environment No problem:

<code>➜  ~ $ php -r "var_dump(putenv('PROJECT_NAME=我')); echo getenv('PROJECT_NAME');"
bool(true)
我 </code>
Copy after login

I also find it quite confusing. I couldn’t find a similar situation after searching on Google. I hope other people can have good answers.


But since the questioner is using Laravel and is not particular about putenv, but just wants to get the value in .env smoothly, you can use some tips:

Add this function in helpers.php of your own project (or other places where custom helper functions are placed, if not, create a new one):

<code>if (! function_exists('menv')) {
    /**
     * Gets the value of an environment variable by getenv() or $_ENV.
     *
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    function menv($key, $default = null)
    {
        if (function_exists('putenv') && function_exists('getenv')) {
            // try to read by getenv()
            $value = getenv($key);

            if ($value === false) {
                return value($default);
            }
        } else {
            // try to read from $_ENV or $_SERVER
            if (isset($_ENV[$key])) {
                $value = $_ENV[$key];
            } elseif (isset($_SERVER[$key])) {
                $value = $_SERVER[$key];
            } else {
                return value($default);
            }
        }

        switch (strtolower($value)) {
            case 'true':
            case '(true)':
                return true;
            case 'false':
            case '(false)':
                return false;
            case 'empty':
            case '(empty)':
                return '';
            case 'null':
            case '(null)':
                return;
        }

        if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
            return substr($value, 1, -1);
        }

        return $value;
    }
}</code>
Copy after login

Customize a new function to obtain environment variables, and replace all env in the config/ directory with menv (I tried $_ENV, but the writing of $_SERVER does not work) Affected)

You can refer to this, it was originally done by me to prevent some users from being able to use putenv

Related labels:
source:php.cn
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