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?
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();
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.
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.
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.
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
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With