Accessing environment variables from PHP
Reviewedbyfl
Markdown ↓getenv
Reading environment variables from PHP.
Read environment variables in PHP using getenv() or $_SERVER. Learn the methods supported by Laravel, Craft CMS, and WordPress.
# 1. use the getenv function
echo getenv("MY_ENV_VAR");
# 2. use the global
echo $_SERVER["MY_ENV_VAR"];
php
- The getenv() PHP function accepts a key and returns its value.
- Use the global variable
$_SERVER.
Frameworks and content management systems will parse and use environment variables automatically. Usually .env files are supported for local development. For example, a config file might contain: $dbHost = env('DB_HOST');.
- Laravel: Use the
env()helper function - Craft CMS:
getenv()inconfig/general.php - WordPress:
getenv()to access environment variables inwp-config.php