laravel-php putenv function Chinese cannot use the problem

Source: Internet
Author: User
If you know laravel can look at the above piece, if you do not know, go straight to the dividing line below;

Any of the Env items here take mail_driver as an example
Change to "my" with the ENV function can get to the value of normal;
Instead of "I" with the ENV function can not be obtained normally;

MAIL_DRIVER=我var_dump(env(MAIL_DRIVER''));  // NULLMAIL_DRIVER=我的var_dump(env(MAIL_DRIVER''));  // 我的

Some other Chinese such as "good" will also appear this situation;
Have to say, laravel and profound; study the ENV function source code;
The last problem to locate is;
-------------------------------------------------understand and don't understand the split line of the Laravel frame------------------------------------------------

putenv("PROJECT_NAME=我的");phpinfo();

Environment in phpinfo can be searched for project_name

putenv("PROJECT_NAME=我");phpinfo();

Environment in phpinfo can not be searched project_name

Ask the parents, why is this? How to set the Chinese value without obstacles?

Reply content:

If you know laravel can look at the above piece, if you do not know, go straight to the dividing line below;

Any of the Env items here take mail_driver as an example
Change to "my" with the ENV function can get to the value of normal;
Instead of "I" with the ENV function can not be obtained normally;

MAIL_DRIVER=我var_dump(env(MAIL_DRIVER''));  // NULLMAIL_DRIVER=我的var_dump(env(MAIL_DRIVER''));  // 我的

Some other Chinese such as "good" will also appear this situation;
Have to say, laravel and profound; study the ENV function source code;
The last problem to locate is;
-------------------------------------------------understand and don't understand the split line of the Laravel frame------------------------------------------------

putenv("PROJECT_NAME=我的");phpinfo();

Environment in phpinfo can be searched for project_name

putenv("PROJECT_NAME=我");phpinfo();

Environment in phpinfo can not be searched project_name

Ask the parents, why is this? How to set the Chinese value without obstacles?

I also tried it and found that as long as the call in the CGI environment putenv('PROJECT_NAME=掉') (four bytes, the Japanese kana is the same) will fail false to return, and in the CLI environment is not a problem:

I also feel quite fascinated, Google a bit also can not find similar situation, expect others to have a good answer.

But since the main use is Laravel, and not particularly rigidly putenv , just want to get .env the value of the words, you can use a few tips:

Add this function to your own project helpers.php (or to any other place where you put a custom help function):

if (! function_exists (' menv ')) {/** * Gets the value of a environment variable by getenv () or $_env. * * @param string $key * @param mixed $default * @return Mixed * * function menv ($key, $default = n ull) {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])) {$va            Lue = $_env[$key];            } elseif (Isset ($_server[$key])) {$value = $_server[$key];            } else {return value ($default); }} switch (Strtolower ($value)) {case ' true ': Case ' (true) ': Return TR            Ue            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; }}

Customize a new function to get the environment variable, and config/ replace all of the directories env menv (I tried $_ENV , $_SERVER the write will not be affected)

can refer to here, originally I for some users can not use the processing is just putenv

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.