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:

<?php
$name = $_ENV['USER'];
?>

Example for Reading user’s home directory

<?php
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.

<?php
$path = getenv('PATH');
?>

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

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.