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 $home = getenv("HOME"); // string or false $mode = getenv("APP_ENV") ?: "production";
Try it
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.
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
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, notnull. A missing variable givesfalse. A variable set to an empty string gives"". Compare with=== falsewhen the difference matters.- Values are strings.
getenv("DEBUG")can never be a boolean. Convert withfilter_var($value, FILTER_VALIDATE_BOOLEAN)or compare to known strings. $_ENVis not reliable. Ifvariables_orderlacksE, it is empty. Do not depend on it in libraries; usegetenv(), or load your own config once at startup.putenv()only affects this process and its children. It changes whatgetenv()and programs started withexec()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, nginxfastcgi_paramand Docker-eall end up here. FastCGI variables may appear in$_SERVERtoo. - Secrets. Do not echo
getenv()output orphpinfo()on a public page, and never commit.envfiles.var_dump(getenv())is a quick way to leak every secret. - Case. Names are case-sensitive on Linux and case-insensitive on Windows.
- Use
$_SERVERfor request data. The request environment (headers, paths) lives in$_SERVER, not here. - Libraries. Packages like
vlucas/phpdotenvread a.envfile and callputenv()or fill$_ENVfor you.
Related
$_SERVER,ini_get(),$GLOBALS, php.ini- Lessons: Superglobals, Deployment, Secure coding