wecsam
wecsam

Reputation: 2771

How do I check whether an environment variable is set in PHP?

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

Upvotes: 21

Views: 34040

Answers (4)

Ludwig
Ludwig

Reputation: 3749

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).

Upvotes: 0

wecsam
wecsam

Reputation: 2771

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.

Upvotes: 43

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

you can check like this

if($ip = getenv('REMOTE_ADDR'))
echo $ip; 

getenv() Returns the value of the environment variable.

Upvotes: 4

William Buttlicker
William Buttlicker

Reputation: 6000

This is what you need

    $var = getenv(MYVAR)
    if(isset($var)) {

    } else {

    }

Upvotes: -7

Related Questions