PHP Environment Variables


genenv() function returns the value of a specific environment variable. putenv() sets value of a specific environment variable. Environment variables are global variables.

<?PHP
   getenv("REMOTE_ADDR"); //get IP address of current user
   putenv("temp=usr");  //set an environment variable temp with value usr
   getenv("temp");  //usr
?>

The environment variables can be accessed with GLOBAL variable $_SERVER. However, environment variables set by putenv() can not.
<?PHP
  $_SERVER['REMOTE_ADDR'];   //IP address of current user
   putenv("temp=usr");  //set an environment variable temp with value usr
   echo $_SERVER['temp'];   //Notice: Undefined index: temp in ... 
?>

PHP global environment variables include: $_SERVER, $_GET, $_FILES, $_POST, $_COOKIE, $_SESSION, $_REQUEST, $_ENV.

Some useful predefined global variables include:
<?PHP
 	echo $_SERVER['PHP_SELF'];   //result is: /program/php/environment.php
	echo __FILE__;   //result is:  http://www.endmemo.com/program/php/environment.php
?>