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

4

20

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
Nf answered 5/7, 2013 at 7:34 Comment(0)
N
42

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.

Nf answered 5/7, 2013 at 7:42 Comment(4)
I don't know why, but my PHP (7.1.32) returns an empty string when MYVAR is not defined... I triedkey_exists('MYVAR', getenv()), but it didn't work either, so I ended up checking for !empty(getenv('MYVAR'))...Ordure
For people using DotEnv: values of OFF will be a string and so evaluate to true with this approach.Pincince
What happened if my env variable ENV_VARIABLE=false ?Mariettemarigold
@Mariettemarigold Environment variables are always strings, so setting it to false will result in a string "false".As
S
4

you can check like this

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

getenv() Returns the value of the environment variable.

Schelling answered 5/7, 2013 at 7:38 Comment(6)
Isn't 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
@Nf REMOTE_ADDR was just an example you can pass different environment variableSchelling
I figured out by myself that 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
@Nf I think its not comparison ,assign getenv('REMOTE_ADDR') value to $ip and then checking for $ip .Schelling
I know that it's not a comparison. What I'm saying is that we usually don't do assignments in if conditionals.Nf
@wescam is right, you can't do assignment inside an if conditionChalmer
A
0

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

As answered 3/11, 2023 at 10:38 Comment(0)
M
-7

This is what you need

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

    } else {

    }
Marinelli answered 5/7, 2013 at 7:37 Comment(2)
isset($var) is always true in this case.Nf
Since getvar() returns false when not set, isset will return true always, since $var is set, but set to false.Brain

© 2022 - 2024 — McMap. All rights reserved.