Get an environment variable | PHP Snippets


Snippet

Fetching an environment variable in PHP uses the getenv() method. The following syntax is the way of fetching an environment variable in PHP:

PHP
getenv('DB_HOST');

Additionally, you can store this environment variable in a local variable like that:

PHP
$variable = getenv('DB_HOST');

Outputting the environment variable follows the same logic:

PHP
echo getenv('DB_HOST');
See also

Apache logo. Copyright may apply. How to set an environment variable on Apache using .htaccess
getenv()