Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP $_ENV and getenv()

Reference for PHP $_ENV, getenv() and putenv(): read environment variables, why $_ENV is often empty, and how to keep configuration and secrets out of code.

Environment variables are named strings that the operating system hands to a process when it starts. They are the standard way to pass configuration (database host, API keys, "production or development") into an application without hard-coding it.

PHP offers two ways to read them. getenv() asks the process environment directly and always works. $_ENV is a superglobal array that PHP fills at startup only when variables_order contains the letter E. The sample php.ini-production and php.ini-development files set variables_order = "GPCS", leaving E out, so $_ENV is empty on many servers while getenv() still returns values.

PHP
<?php
$home = getenv("HOME");                 // string or false
$mode = getenv("APP_ENV") ?: "production";

Try it

PHP getenv(), putenv() and the false result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP getenv() without arguments and $_ENV

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
getenv(?string $name = null, bool $local_only = false): string|array|false
putenv(string $assignment): bool
$_ENV["NAME"]
Name Description
$name The variable name. If null or omitted, getenv() returns an array of all variables.
$local_only If true, only the environment of PHP's own process is read, not the web server's SAPI environment.
$assignment "NAME=value" sets a variable; "NAME" (no =) removes it.

Return value: getenv("NAME") returns the value as a string, or false if the variable is not defined. putenv() returns true on success.

Usage notes

  • false, not null. A missing variable gives false. A variable set to an empty string gives "". Compare with === false when the difference matters.
  • Values are strings. getenv("DEBUG") can never be a boolean. Convert with filter_var($value, FILTER_VALIDATE_BOOLEAN) or compare to known strings.
  • $_ENV is not reliable. If variables_order lacks E, it is empty. Do not depend on it in libraries; use getenv(), or load your own config once at startup.
  • putenv() only affects this process and its children. It changes what getenv() and programs started with exec() see. It never writes to $_ENV, and it does not outlive the request.
  • Thread safety. In multithreaded SAPIs (some Apache and Windows setups) putenv() is not safe to use; prefer your own config array.
  • Web servers add variables. Apache SetEnv, nginx fastcgi_param and Docker -e all end up here. FastCGI variables may appear in $_SERVER too.
  • Secrets. Do not echo getenv() output or phpinfo() on a public page, and never commit .env files. var_dump(getenv()) is a quick way to leak every secret.
  • Case. Names are case-sensitive on Linux and case-insensitive on Windows.
  • Use $_SERVER for request data. The request environment (headers, paths) lives in $_SERVER, not here.
  • Libraries. Packages like vlucas/phpdotenv read a .env file and call putenv() or fill $_ENV for you.