getenv()不会显示所有变量,即使已设置它们也是如此 [英] getenv() doesn't show all variables, even if they are set
问题描述
我正在测试在.htaccess中设置环境变量供我的php代码使用,并面临 getenv()
函数的奇怪行为.
I'm testing setting environment variables in .htaccess for my php code to use, and facing strange behaviour from the getenv()
function.
在.htaccess中,我设置了
In my .htaccess, I set
SetEnv BOP 23
在我的php中,我成功获得
And in my php, I successfully get
$res = getenv("BOP");
echo($res);
>>>> 23
但是当我尝试使用 var_dump(getenv());
或使用调试器查看 getenv()
返回的数组中包含什么时,我得到了长度23但我的"BOP"变量丢失了.
But when I try to var_dump(getenv());
or use debugger to see what is in the array returned by getenv()
, I get an array of length 23 BUT my "BOP" variable is missing.
例如,如果我尝试将 getenv()
转储到数组中并进行查找:
For example, if I try to dump getenv()
into an array and make a lookup:
$array = getenv();
echo($array["BOP"])
我得到一个例外:
Exception has occurred.
Notice: Undefined index: BOP
根据我在此处阅读的有关 getenv()
的信息 https://www.php.net/manual/zh/function.getenv.php
...如果省略
varname
,则所有环境变量将作为关联数组返回.
... If
varname
is omitted, all environment variables are returned as associative array.
那我想念什么?为什么我可以访问变量,但不是在 getenv()
中?
So what am I missing? Why can I access my variable but isn't it in getenv()
?
推荐答案
请确保使用PHP 7.1或更高版本
Be shure you use PHP 7.1 or higher
从7.1.0版getenv()不再需要其参数.如果省略该参数,那么当前环境变量将作为关联数组返回.
FROM 7.1.0 getenv() no longer requires its parameter. If the parameter is omitted, then the current environment variables will be returned as an associative array.
这篇关于getenv()不会显示所有变量,即使已设置它们也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!