-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Muhammet Şafak edited this page Jun 8, 2026
·
2 revisions
Welcome to the official documentation for initphp/dotenv — a small,
dependency-free environment-variable loader for PHP 8.0+.
It reads a .env (or .env.php) file, pushes the values into PHP's
environment ($_ENV, $_SERVER, getenv()), and reads them back with
type coercion and ${VAR} interpolation — without ever overwriting a
real environment variable.
composer require initphp/dotenvuse InitPHP\DotENV\DotENV;
DotENV::create(__DIR__); // loads __DIR__/.env (or .env.php)
DotENV::get('APP_ENV', 'local'); // → string|int|float|bool|null
env('APP_ENV', 'local'); // global helper, same shared stateGiven this .env:
APP_ENV = production
APP_DEBUG = false
DB_PORT = 5432
SITE_URL = https://example.test
PAGE_URL = ${SITE_URL}/page
ZIP_CODE = 007you get correctly typed values back:
DotENV::get('APP_ENV'); // "production" (string)
DotENV::get('APP_DEBUG'); // false (bool)
DotENV::get('DB_PORT'); // 5432 (int)
DotENV::get('PAGE_URL'); // "https://example.test/page" (interpolated)
DotENV::get('ZIP_CODE'); // "007" (string — leading zero preserved)| Symbol | Role |
|---|---|
DotENV |
Static facade over a shared repository — the usual entry point. |
Repository |
The worker doing the loading/parsing/reading; use directly for an isolated instance. |
env() |
Global helper function, equivalent to DotENV::get(). |
DotENVException |
The single exception type, extends \InvalidArgumentException. |
- New to the package? Read Installation, then Quick Start.
-
Writing a
.envfile? The.envFile Format spells out every parsing rule. -
Wondering why
007stays a string? Value Types & Coercion. -
Chaining values with
${VAR}? Variable Interpolation. - Coming from 2.x? Read Migration from 2.x — 3.0 raises the PHP floor and changes value coercion.
- Looking for a specific method? The full API Reference lists every public member.
-
Two file formats. Plain-text
.envor a.env.phpthat returns a native PHP array. -
Smart, loss-free typing.
true/false/null/emptybecome real PHP values; numbers becomeint/floatonly when it round-trips, so007,+90555…, oversized integers and1e3stay strings. See Value Types & Coercion. -
${VAR}interpolation. References are resolved on read, support multiple and nested references on one line, and never recurse forever on a cycle. See Variable Interpolation. -
Real env vars always win.
create()never overwrites a name already defined in$_ENV,$_SERVER, orgetenv(). See Loading & Precedence. -
One exception type. Every failure raises
DotENVException; onecatchcovers everything, and$debug = falseturns failures into silent no-ops. See Error Handling. -
Test-friendly.
flush()/reset()unload exactly what was loaded, leaving the real environment untouched. See Testing.
| Input in the file |
get() returns |
|---|---|
KEY=hello |
"hello" (string) |
KEY=true / KEY=false
|
true / false (bool) |
KEY=null |
null |
KEY=empty or KEY=
|
"" (string) |
KEY=42 / KEY=-3.5
|
42 (int) / -3.5 (float) |
KEY=007 / KEY=+15551234
|
"007" / "+15551234" (string) |
KEY=" spaced " |
" spaced " (quotes stripped, content verbatim) |
KEY=#ffffff |
"#ffffff" (leading # kept) |
KEY=val # note |
"val" (inline comment stripped) |
KEY=${OTHER}/x |
OTHER's value + /x (interpolated) |
- License: MIT
- Minimum PHP: 8.0
- Required extensions: none beyond the PHP core
- Runtime dependencies: none
-
Packagist:
initphp/dotenv - Source: github.com/InitPHP/DotENV
- Issues: github.com/InitPHP/DotENV/issues
- Discussions: github.com/orgs/InitPHP/discussions
If something in this wiki is unclear, wrong, or out of date, open an issue — documentation fixes are merged eagerly.
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