It is interesting. After investigating, I found that there is an undocumented parameter for getenv() .
A call to putenv("TESTVAR_ENV=new-value") , followed by getenv("TESTVAR_ENV", true) , returns new-value , as expected. However, getenv("TESTVAR_ENV", true) returns false when called without explicitly setting the value first.
Reading from the source it seems that if local_only is set to false (the default), the value is selected using sapi_getenv , while with local_only set to true, native getenv .
In addition, if sapi_getenv does not return a value, then getenv is called as a reserve. Value, if you do not set TESTVAR_ENV in the nginx / Apache configuration at all, putenv / getenv works as putenv .
So, to repeat:
getenv(name) searches from the table of the internal SAPI environment (php-fpm) and returns to the OS environment if the variable is not set.getenv(name, true) searches only from the OS environment, which optionally (depending on SAPI) contains variables registered in the web server configuration.putenv() always updates only the OS.
I used the following to verify this:
header("Content-Type: text/plain"); dump_env(); echo 'getenv("TESTVAR_ENV") => ' . var_export(getenv("TESTVAR_ENV"), true) . "\n"; echo 'getenv("TESTVAR_ENV", true) => ' . var_export(getenv("TESTVAR_ENV", true), true) . "\n"; echo "-----------\n"; echo 'putenv("TESTVAR_ENV=new-value") => ' . var_export(putenv("TESTVAR_ENV=new-value"), true) . "\n"; dump_env(); echo 'getenv("TESTVAR_ENV") => ' . var_export(getenv("TESTVAR_ENV"), true) . "\n"; echo 'getenv("TESTVAR_ENV", true) => ' . var_export(getenv("TESTVAR_ENV", true), true) . "\n"; function dump_env() { echo "--- env ---\n" . `env` . "-----------\n"; }
Joe
source share