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.
you can check like this
if($ip = getenv('REMOTE_ADDR'))
echo $ip;
getenv() Returns the value of the environment variable.
This is what you need
$var = getenv(MYVAR)
if(isset($var)) {
} else {
}