solophp / dotenv
Simple, zero-dependency .env loader for PHP. Loads files into $_ENV with optional $_SERVER population.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/solophp/dotenv
Requires
- php: ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.10
README
Simple, zero-dependency .env loader for PHP. Loads environment variables from files into $_ENV (optionally also $_SERVER). No global putenv calls, no magic variable expansion.
Features
- Small and focused: just parsing and loading
- Zero dependencies
- Deterministic overrides: later files in the same call override earlier ones
- Typed accessors:
string,int,bool, andrequired
Installation
composer require solophp/dotenv
Quick start
use Solo\Dotenv\Dotenv; Dotenv::load(__DIR__, ['.env', '.env.local']); $dbDsn = Dotenv::required('DB_DSN'); $isDebug = Dotenv::bool('APP_DEBUG', false);
API
Dotenv::load(string $baseDir, array $filenames = ['.env', '.env.local'], bool $overwrite = false, bool $populateServer = false): void- Loads files in order. Existing environment variables set before the call are preserved by default. Variables set by earlier files in the same call can be overridden by later files.
- Set
$overwrite = trueto overwrite any existing$_ENVvalues. - Set
$populateServer = trueto mirror values into$_SERVER.
Dotenv::get(string $key, mixed $default = null): mixedDotenv::string(string $key, string $default = ''): stringDotenv::int(string $key, int $default = 0): intDotenv::bool(string $key, bool $default = false): boolDotenv::required(string $key): string— throwsInvalidArgumentExceptionif missing/empty.
Parser
The parser is implemented in Solo\Dotenv\EnvParser and supports:
- Lines like
KEY=VALUE - Optional leading
export - Single and double quotes
- Inline comments for unquoted values (
FOO=bar # comment) - No variable expansion (by design)
Testing
composer install
composer test
License
MIT