-
Notifications
You must be signed in to change notification settings - Fork 1
The env File Format
A .env file is a list of KEY=VALUE lines. This page documents exactly
how each line is parsed, with the edge cases spelled out.
# Application settings
APP_NAME = InitPHP
APP_ENV = production
DB_HOST = 127.0.0.1
DB_PORT = 5432Reading happens lazily: the file is parsed once at create() time, but type
coercion and ${VAR} interpolation happen when you call
get(). This page is about the line grammar; for
what the value turns into, see Value Types & Coercion
and Variable Interpolation.
The line is split on the first =. Everything before it is the key,
everything after is the value.
DSN = mysql:host=localhost;dbname=app
# └── value keeps every '=' after the first one-
Whitespace around the key is trimmed:
FOO = barandFOO=barare the same. -
An optional leading
exportis stripped, so a file that is alsosource-able by a shell works:export TOKEN = abc123 # key is TOKEN
The prefix must be the literal word
exportfollowed by a single space.export\tFOO(a tab) is not treated as the prefix.
A line is a comment when its first character (after trimming) is anything
other than a letter, a digit, _, or -. So every one of these is ignored:
# hash comment
; semicolon comment
// double-slash comment
* star commentBlank lines are ignored too.
An inline comment begins at the first # that is preceded by whitespace.
The # and everything after it are dropped:
URL = https://example.test # everything here is a comment
# └── value becomes "https://example.test"A # that is not preceded by whitespace is part of the value. This keeps
hex colours, URL fragments and the like intact:
COLOR = #ffffff # value: "#ffffff"
FRAGMENT = page#section # value: "page#section"Why the whitespace rule? Without it,
COLOR=#ffffffwould be read as an empty value followed by a comment. Requiring a space before#lets a value legitimately start with#.
Wrap a value in matching single (') or double (") quotes to take it
verbatim. The surrounding quotes are removed; everything between them —
including a #, spaces, or the other quote character — is preserved.
GREETING = "hello world" # value: hello world
PADDED = " keep my spaces " # value: " keep my spaces " (inner spaces kept)
HASH = "#ffffff" # value: #ffffff
APOS = "it's fine" # value: it's fineQuoting works even when there is whitespace around the =:
SITE = "https://example.test" # value: https://example.testAn inline comment is still recognised after the closing quote:
TOKEN = "abc" # secret # value: abcEscape sequences are not processed inside quotes — \n, \t and \" are
kept literally. If you need real newlines or tabs in a value, set them another
way (for example through a .env.php file).
Leading and trailing whitespace is trimmed from both the key and the value:
SPACED = value with trailing spaces
# └ key: "SPACED", value: "value with trailing spaces"To preserve leading/trailing spaces in a value, quote it (see above).
A non-comment line with no = is ignored rather than producing a junk
entry. It does not raise a warning:
THIS_LINE_IS_IGNORED
VALID = 1 # only VALID is defined| Line | Parsed key | Parsed value |
|---|---|---|
FOO=bar |
FOO |
bar |
FOO = bar |
FOO |
bar |
export FOO=bar |
FOO |
bar |
FOO="a b" |
FOO |
a b |
FOO='a b' |
FOO |
a b |
FOO=a # c |
FOO |
a |
FOO=#abc |
FOO |
#abc |
FOO=a#b |
FOO |
a#b |
FOO=mysql:a=b |
FOO |
mysql:a=b |
# comment |
— | (ignored) |
; comment |
— | (ignored) |
NO_EQUALS_HERE |
— | (ignored) |
| (blank) | — | (ignored) |
- Value Types & Coercion — what the parsed string becomes when you read it
-
Variable Interpolation —
${VAR}references -
Using a PHP
.envFile — native types via a PHP array
initphp/dotenv · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
The .env Format
Core Concepts
Practical Guides
Other