k0pernikus
k0pernikus

Reputation: 66730

How to unset an environment variable using php?

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.

I have a feature set that may be 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

export ENV_FOO=something`

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

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

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?

Upvotes: 13

Views: 10859

Answers (1)

Alex Andrei
Alex Andrei

Reputation: 7283

Try this from the putenv docpage comments

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

Upvotes: 32

Related Questions