Get the value of an environment variable in PHP

Reading environment variables is possible in PHP by using the $_ENV auto-global array. They are named values associated with a process. For example, the value of $_ENV['HOME'] can be used in unix to find the home directory of a user.

Example for Reading an environment variable:

$name = $_ENV['USER'];

Example for Reading user’s home directory

print $_ENV['HOME']; // user's home directory

The $_ENV array is created only if the value of the variables_order configuration directive contains E. If $_ENV isn’t available, use getenv() to retrieve an environment variable.

$path = getenv('PATH');

The getenv() function isn’t available if you’re running PHP as an ISAPI module.

Share Article/Example:
  • Facebook
  • Twitter
  • Digg
  • DotNetKicks
  • DZone