bash 如何使用php取消设置环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34065428/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:59:03  来源:igfitidea点击:

How to unset an environment variable using php?

phpbashenvironment-variableshhvm

提问by k0pernikus

I know I can set an environment variable using

我知道我可以使用设置环境变量

putenv("ENV_FOO=SOMETHING");

and get the value via:

并通过以下方式获取价值:

getenv("ENV_FOO");

If the variable isn't set, getenv("ENV_FOO")will return false.

如果未设置变量,getenv("ENV_FOO")将返回false.

I have a feature set that maybe set via an environment variable, and I wanted to unit test the behavior when the variable is set or not set.

我有一个可以通过环境变量设置的功能集,我想在设置或未设置变量时对行为进行单元测试。

Yet once export the variable on my dev machine in bash via

然而,一旦在我的开发机器上通过 bash 导出变量

export ENV_FOO=something`

it breaks my unit test, as I cannot unset the environment variable using php for the scope of the test.

它破坏了我的单元测试,因为我无法在测试范围内使用 php 取消设置环境变量。

I tried putenv("ENV_FOO=");yet this will return in an empty string "", not in an unset environment variable for the current shell session.

我试过putenv("ENV_FOO=");这将返回一个空字符串"",而不是当前 shell 会话的未设置环境变量。

Is there a way to unset an environment variable for the current shell session, or do I have to change the way I test for the existence of the variable?

有没有办法为当前 shell 会话取消设置环境变量,或者我是否必须更改测试变量是否存在的方式?

回答by Alex Andrei

Try this from the putenvdocpage comments

putenv文档页面评论中试试这个

<?php
putenv('MYVAR='); // set MYVAR to an empty value.  It is in the environment
putenv('MYVAR'); // unset MYVAR.  It is removed from the environment
?>