事实证明,这里有两个问题:
1. 只有在 php.ini
允许的情况下才会填充 $_ENV,默认情况下似乎没有这样做,至少在默认的 WAMP 服务器安装中不会这样做。
; This directive determines which super global arrays are registered when PHP
; starts up. If the register_globals directive is enabled, it also determines
; what order variables are populated into the global space. G,P,C,E & S are
; abbreviations for the following respective super globals: GET, POST, COOKIE,
; ENV and SERVER. There is a performance penalty paid for the registration of
; these arrays and because ENV is not as commonly used as the others, ENV is
; is not recommended on productions servers. You can still get access to
; the environment variables through getenv() should you need to.
; Default Value: "EGPCS"
; Development Value: "GPCS"
; Production Value: "GPCS";
; http://php.net/variables-order
variables_order = "GPCS"
当我将variables_order
设置回 ,不再是空的。EGPCS
$_ENV
2.当您在.htaccess
中使用SetEnv
时,它最终以$ _SERVER
而不是$ _ENV
结束,我不得不说,当它被命名为时有点令人困惑...SetEnv
# .htaccess
SetEnv ENV dev
SetEnv BASE /ssl/
# php
var_dump($_SERVER['ENV'], $_SERVER['BASE']);
// string 'dev' (length=3)
// string '/ssl/' (length=5)
3. getenv
函数将始终工作,不受 PHP 设置为 $_ENV 的影响 此外,它似乎对大小写不敏感,这可能很有用。
var_dump(getenv('os'), getenv('env'));
// string 'Windows_NT' (length=10)
// string 'dev' (length=3)