Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 489
k0pernikus Avatar asked Dec 03 '15 11:12

k0pernikus


People also ask

How do you unset a variable in PHP?

PHP | unset() Function It means that this function unsets only local variable. If we want to unset the global variable inside the function then we have to use $GLOBALS array to do so. Return Value: This function does not returns any value. unset_value();

Is unset variable used in PHP?

The unset() function is a predefined variable handling function of PHP, which is used to unset a specified variable. In other words, "the unset() function destroys the variables". The behavior of this function varies inside the user-defined function.

How do you unset a variable?

To unset the variable simply set the value of variable to '' . c.) Here, we created a local variable VAR2 and set it to a value. Then in-order to run a command temporarily clearing out all local and other environment variables, we executed 'env –i' command.

What is meant by unset in PHP?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.


1 Answers

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
?>
like image 70
Alex Andrei Avatar answered Oct 17 '22 03:10

Alex Andrei