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>
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>
You can search for PROJECT_NAME in Environment in phpinfo
<code>putenv("PROJECT_NAME=我"); phpinfo();</code>
Project_NAME cannot be found in Environment in phpinfo
Dear friends, why is this? How to set Chinese value without any problem?
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>
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>
You can search for PROJECT_NAME in Environment in phpinfo
<code>putenv("PROJECT_NAME=我"); phpinfo();</code>
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>
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>
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