Cookies ahead

Our support chat tool "Intercom" would like to collect some more data on you. See the related link for more details.

Docs

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
  1. The getenv() PHP function accepts a key and returns its value.
  2. 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() in config/general.php
  • WordPress: getenv() to access environment variables in wp-config.php

Written by a human. Review, grammar checks and typo fixes by AI.

AI use & editorial processEdit on GitHub ↗