In PHP, how do I test whether an environment variable is set? I would like behavior like this:
// Assuming MYVAR isn't defined yet.
isset(MYVAR); // returns false
putenv("MYVAR=foobar");
isset(MYVAR); // returns true
In PHP, how do I test whether an environment variable is set? I would like behavior like this:
// Assuming MYVAR isn't defined yet.
isset(MYVAR); // returns false
putenv("MYVAR=foobar");
isset(MYVAR); // returns true
getenv()
returns false
if the environment variable is not set. The following code will work:
// Assuming MYVAR isn't defined yet.
getenv("MYVAR") !== false; // returns false
putenv("MYVAR=foobar");
getenv("MYVAR") !== false; // returns true
Be sure to use the strict comparison operator (!==
) because getenv()
normally returns a string that could be cast as a boolean.
true
with this approach. –
Pincince ENV_VARIABLE=false
? –
Mariettemarigold you can check like this
if($ip = getenv('REMOTE_ADDR'))
echo $ip;
getenv() Returns the value of the environment variable.
REMOTE_ADDR
always set? In my case, I'm writing a script that needs to act differently based on whether the server has an environment variable set. –
Nf getenv()
returns false
if the environment variable is not set. I see that that is what your code tries to illustrate. However, there are several problems with your code. First of all, we try to avoid using the =
assignment operator in if
conditionals because we almost always meant ==
. Second of all, the value of the environment variable could be an empty string that casts to false
, so we need to use a strict comparison operator. –
Nf if
conditionals. –
Nf Environment variables (at least in PHP) are always strings. It is not possible to set them to boolean false
- that's why putenv()
only accepts one string as a parameter. So whenever getenv()
returns false
the environment variable is not set. Knowing this you can find out if an environment variable is set by strictly comparing it against false
.
if ( getenv('MYVAR') !== false ) {
echo 'Environment variable "MYVAR" is set to "' . getenv('MYVAR') . '"';
}
Here are some tests (using the Apache directive SetEnv
):
SetEnv TEST_FALSE false
SetEnv TEST_QUOTED_FALSE "false"
SetEnv TEST_0 0
SetEnv TEST_QUOTED_0 "0"
SetEnv TEST_EMPTY
SetEnv TEST_QUOTED_EMPTY ""
and PHP putenv()
:
putenv("PUTENV_FALSE=false");
putenv("PUTENV_0=0");
putenv("PUTENV_EMPTY=");
what will result in:
UNSET_ENV: bool(false)
TEST_FALSE: string(5) "false"
TEST_QUOTED_FALSE: string(5) "false"
TEST_0: string(1) "0"
TEST_QUOTED_0: string(1) "0"
TEST_EMPTY: string(0) ""
TEST_QUOTED_EMPTY: string(0) ""
PUTENV_FALSE: string(5) "false"
PUTENV_0: string(1) "0"
PUTENV_EMPTY: string(0) ""
Not asked here, but related to it:
If you want to check an environment variable if it is set to boolean true, you could use the filter functions:
filter_var( getenv('MYVAR'), FILTER_VALIDATE_BOOLEAN) );
Because when using this filter, the result would look like that:
UNSET_ENV: bool(false)
TEST_FALSE: bool(false)
TEST_QUOTED_FALSE: bool(false)
TEST_0: bool(false)
TEST_QUOTED_0: bool(false)
TEST_EMPTY: bool(false)
TEST_QUOTED_EMPTY: bool(false)
PUTENV_FALSE: bool(false)
PUTENV_0: bool(false)
PUTENV_EMPTY: bool(false)
Where values like "true", "on", "yes" and "1" (all case insensitive) would result in bool(true)
. Any other string is returning a bool(false)
.
This is what you need
$var = getenv(MYVAR)
if(isset($var)) {
} else {
}
isset($var)
is always true in this case. –
Nf © 2022 - 2024 — McMap. All rights reserved.
key_exists('MYVAR', getenv())
, but it didn't work either, so I ended up checking for!empty(getenv('MYVAR'))
... – Ordure