简体   繁体   中英

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

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 {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM