ez-php / dotenv
Lightweight .env file loader for PHP — supports quoted values, variable interpolation, and immutable loading
0.2.0
2026-03-15 03:47 UTC
Requires
- php: ^8.5
Requires (Dev)
- 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.
License
MIT — Andreas Uretschnig