ez-php / dotenv
Lightweight .env file loader for PHP — supports quoted values, variable interpolation, and immutable loading
Requires
- php: ^8.5
Requires (Dev)
- ez-php/docker: ^1.0
- ez-php/testing-application: ^1.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
README
Lightweight .env file loader for PHP — supports quoted values, variable interpolation, and immutable loading. Zero dependencies.
Requirements
- PHP 8.5+
Installation
composer require ez-php/dotenv
Usage
use EzPhp\Env\Dotenv; // Load .env from the project root (immutable — never overwrites existing env vars) Dotenv::createImmutable(__DIR__)->load(); // Silently skip if the file doesn't exist (useful in production) Dotenv::createImmutable(__DIR__)->safeLoad(); // Custom filename Dotenv::createImmutable(__DIR__, '.env.testing')->load();
Variables are written to $_ENV, $_SERVER, and getenv().
Supported syntax
# Comments are ignored APP_NAME=My App APP_ENV=production # Quoted values preserve whitespace and support escapes GREETING="Hello, World!\nWelcome." # Single-quoted values are literal — no interpolation, no escapes LITERAL='value with $dollar and \n backslash' # Variable interpolation in double-quoted values BASE_URL=https://example.com API_URL="${BASE_URL}/api/v1" # export prefix is stripped export SECRET_KEY=abc123 # Empty values EMPTY=
Immutability
Variables already present in the environment (set before loading) are never overwritten. This means real environment variables (e.g. set in Docker or CI) always take precedence over .env file values.
Parser
Parser is the internal class that turns raw .env file content into a flat array<string, string> map. You can use it directly if you want to parse .env content without writing to the environment:
use EzPhp\Env\Parser; $vars = (new Parser())->parse(file_get_contents('.env')); // ['APP_NAME' => 'My App', 'APP_ENV' => 'production', ...]
All syntax forms are supported: unquoted, double-quoted (escape sequences + interpolation), single-quoted (literal), empty values, export prefix, and inline comments.
License
MIT — Andreas Uretschnig