简体   繁体   English

如何检查是否在PHP中设置了环境变量?

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

In PHP, how do I test whether an environment variable is set? 在PHP中,如何测试是否设置了环境变量? 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. 如果未设置环境变量, getenv()将返回false 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. 一定要使用严格比较运算符( !== ),因为getenv()通常返回一个可以作为布尔值转换的字符串。

you can check like this 你可以这样检查

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

getenv () Returns the value of the environment variable. getenv ()返回环境变量的值。

This is what you need 这就是你需要的

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

    } else {

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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