PHP 中的 getenv() 与 $_ENV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8798294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getenv() vs. $_ENV in PHP
提问by tau
What is the difference between getenv()
and $_ENV
?
getenv()
和 和有$_ENV
什么区别?
Any trade-offs between using either?
使用两者之间的任何权衡?
I noticed sometimes getenv()
gives me what I need, while $_ENV
does not (such as HOME
).
我注意到有时getenv()
会给我我需要的东西,而$_ENV
不会(例如HOME
)。
采纳答案by Batkins
According to the php documentation about getenv, they are exactly the same, except that getenv
will look for the variable in a case-insensitive manner. Most of the time it probably doesn't matter, but one of the comments on the documentation explains:
根据有关 getenv 的 php 文档,它们完全相同,只是getenv
将以不区分大小写的方式查找变量。大多数情况下,这可能无关紧要,但文档中的评论之一解释说:
For example on Windows $_SERVER['Path'] is like you see, with the first letter capitalized, not 'PATH' as you might expect.
例如,在 Windows 上 $_SERVER['Path'] 就像您看到的那样,第一个字母大写,而不是您所期望的 'PATH'。
Because of that, I would probably opt to use getenv
unless you are certain about the casing of the title of the variable you are trying to retrieve.
因此,getenv
除非您确定要检索的变量标题的大小写,否则我可能会选择使用。
回答by Conor McDermottroe
I know that the comment in the docs says that getenv
is case-insensitive, but that's notthe behaviour I'm seeing:
我知道文档中的评论说这getenv
是不区分大小写的,但这不是我看到的行为:
> env FOO=bar php -r 'print getenv("FOO") . "\n";'
bar
> env FOO=bar php -r 'print getenv("foo") . "\n";'
> env foo=bar php -r 'print getenv("foo") . "\n";'
bar
> env foo=bar php -r 'print getenv("FOO") . "\n";'
> php --version
PHP 5.4.24 (cli) (built: Jan 24 2014 03:51:25)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Looking at the source codefor the getenv
function, this is because there are three ways that PHP can fetch the environment variable:
纵观源代码的getenv
功能,这是因为有三种方法PHP可以获取环境变量:
- Via
sapi_getenv
(e.g. if it's getting the environment variable from Apache) - If on Windows, from
GetEnvironmentVariableA
. - If on non-Windows, from the
getenv
function provided bylibc
.
- 通过
sapi_getenv
(例如,如果它从 Apache 获取环境变量) - 如果在 Windows 上,从
GetEnvironmentVariableA
. - 如果在非Windows,从
getenv
所提供的功能libc
。
As far as I can tell, the only time when it will behave in a case-insensitive manner is on Windows because that's how the Windows environment variable API behaves. If you're on Linux, BSD, Mac, etc then getenv
is still case sensitive.
据我所知,它以不区分大小写的方式运行的唯一时间是在 Windows 上,因为这就是 Windows 环境变量 API 的行为方式。如果您使用的是 Linux、BSD、Mac 等,那么getenv
仍然区分大小写。
As mentioned by mario, $_ENV
is not always populated due to different configurations of variables_order
so it's best if you avoid $_ENV
if you don't control the server configuration.
正如mario所提到的,$_ENV
由于不同的配置,并不总是填充,variables_order
因此$_ENV
如果您不控制服务器配置,最好避免。
So, for the most portable PHP code:
因此,对于最便携的 PHP 代码:
- Use
getenv
. - Use the correct case for the environment variable name.
- 使用
getenv
. - 对环境变量名称使用正确的大小写。
回答by mario
Additionally $_ENV
is typically empty if variables_order
does't have E
listed. On many setups it's likely that only $_SERVER
is populated, and $_ENV
is strictly for CLI usage.
另外$_ENV
通常是空的,如果variables_order
简化版,已经E
上市。在许多设置中,可能只$_SERVER
填充,并且$_ENV
严格用于 CLI 使用。
On the other hand getenv()
accesses the environment directly.
另一方面getenv()
直接访问环境。
(Regarding the case-ambiguity, one could more simply employ array_change_key_case()
.)
(关于大小写歧义,可以更简单地使用array_change_key_case()
。)
回答by Leopoldo Sanczyk
I found getenv()
useful to avoid a strange PHP bugwhere sometimes $_SERVER
and $_ENV
was undefined if auto_globals_jit
was enabled (creating the _SERVERand _ENVvariables when they're first used). Since then I began to to use it.
我发现getenv()
避免一个奇怪的 PHP 错误很有用,如果启用了有时$_SERVER
并且$_ENV
未定义的错误auto_globals_jit
(在第一次使用时创建_SERVER和_ENV变量)。从那以后我开始使用它。
回答by Joe Green
I'd add that getenv() is a better choice because, as a function, it can be overloaded for testing purposes. Whereas overwriting your $_SERVER or $_ENV variables might interfere with test frameworks and other libraries and ultimately require a lot more work to carry out safely.
我要补充一点, getenv() 是一个更好的选择,因为作为一个函数,它可以被重载以进行测试。而覆盖您的 $_SERVER 或 $_ENV 变量可能会干扰测试框架和其他库,最终需要做更多的工作才能安全地执行。
回答by Ayman Safadi
Taken from the PHP docs:
取自PHP 文档:
This function is useful (compared to
$_SERVER
,$_ENV
) because it searches $varname key in those array case-insensitive manner. For example on Windows$_SERVER['Path']
is like you see Capitalized, not 'PATH
' as you expected. So just:<?php getenv('path') ?>
此函数很有用(与
$_SERVER
,相比$_ENV
),因为它以不区分大小写的数组方式搜索 $varname 键。例如,在 Windows$_SERVER['Path']
上就像您看到的是大写字母,而不是PATH
您预期的' '。所以就:<?php getenv('path') ?>